From: Andrew Morton <akpm@linux-foundation.org>
To: Liu Sha <rednoax@qq.com>
Cc: linux-kernel@vger.kernel.org, rednoax@gmail.com
Subject: Re: [PATCH] lib/idr.c: remove duplicated bound checking in sub_alloc
Date: Fri, 20 Mar 2015 14:28:03 -0700 [thread overview]
Message-ID: <20150320142803.c2f096eb607df8333a60dd25@linux-foundation.org> (raw)
In-Reply-To: <1426787827-1424-1-git-send-email-rednoax@qq.com>
On Thu, 19 Mar 2015 17:57:07 +0000 Liu Sha <rednoax@qq.com> wrote:
> From: Liu Sha <rednoax@gmail.com>
>
> The INT_MAX bound checking in sub_alloc checks two conditions to see
> whether the signed integer "id" is beyond INT_MAX:
>
> if ((id >= MAX_IDR_BIT) || (id < 0))
> return -ENOSPC;
>
> These two conditions are actually the same for "int" variable so one
> of them can be removed. If the above snippet is compiled with -Os option
> of gcc, only one checking will remain in disassembly code.
>
> --- a/lib/idr.c
> +++ b/lib/idr.c
> @@ -262,7 +262,7 @@ static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa,
> sh = IDR_BITS*l;
> id = ((id >> sh) ^ n ^ m) << sh;
> }
> - if ((id >= MAX_IDR_BIT) || (id < 0))
> + if (id >= MAX_IDR_BIT)
> return -ENOSPC;
> if (l == 0)
> break;
Well. This only works because MAX_IDR_BIT happens to have unsigned
type, so the comparison is done with unsigned arithmetic.
The patch makes no difference to code size with my gcc and I'm inclined
to leave the code as-is for reasons of safety and clarity.
next parent reply other threads:[~2015-03-20 21:28 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <1426787827-1424-1-git-send-email-rednoax@qq.com>
2015-03-20 21:28 ` Andrew Morton [this message]
2015-03-21 7:47 ` Liu Sha
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20150320142803.c2f096eb607df8333a60dd25@linux-foundation.org \
--to=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rednoax@gmail.com \
--cc=rednoax@qq.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®