All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <sasha.levin@oracle.com>
To: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] linux/log2.h: Fix roundup_pow_of_two(0)
Date: Thu, 3 Dec 2015 11:04:53 -0500	[thread overview]
Message-ID: <56606825.4000105@oracle.com> (raw)
In-Reply-To: <CAPAsAGzmWjXJmkn+krACt5GWfyNEhEpPJdmgV4x=UkKhUnZH+A@mail.gmail.com>

On 12/03/2015 10:39 AM, Andrey Ryabinin wrote:
> 2015-12-03 18:30 GMT+03:00 Sasha Levin <sasha.levin@oracle.com>:
>> > Passing 0 to roundup_pow_of_two would lead to wrapping around and trying to
>> > find the last set bit on (unsigned long)(-1), which is obviously wrong.
>> >
>> > Instead, deal with this case by rounding it up to the closest power of two
>> > (2 ** 0).
>> >
>> > Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
>> > ---
>> >  include/linux/log2.h |    3 +++
>> >  1 file changed, 3 insertions(+)
>> >
>> > diff --git a/include/linux/log2.h b/include/linux/log2.h
>> > index fd7ff3d..b6bdf0c 100644
>> > --- a/include/linux/log2.h
>> > +++ b/include/linux/log2.h
>> > @@ -60,6 +60,9 @@ bool is_power_of_2(unsigned long n)
>> >  static inline __attribute__((const))
>> >  unsigned long __roundup_pow_of_two(unsigned long n)
>> >  {
>> > +       if (n == 0)
>> > +               return 1UL << 0;
>> > +
> Perhaps we should fix callers instead?
> Comment near roundup_pow_of_two() says that result is undefined when n == 0:

That's how I've started doing it, but when it showed up with 3 different callers
I figured it's better to fix it at the source.

This fix would return a valid value and is working fine with the callers.


Thanks,
Sasha

  reply	other threads:[~2015-12-03 16:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-03 15:30 [PATCH] linux/log2.h: Fix roundup_pow_of_two(0) Sasha Levin
2015-12-03 15:39 ` Andrey Ryabinin
2015-12-03 16:04   ` Sasha Levin [this message]
2015-12-03 16:15     ` Andrey Ryabinin

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=56606825.4000105@oracle.com \
    --to=sasha.levin@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ryabinin.a.a@gmail.com \
    --cc=torvalds@linux-foundation.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.