lkml.org 
[lkml]   [2015]   [Dec]   [3]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
SubjectRe: [PATCH] linux/log2.h: Fix roundup_pow_of_two(0)
From
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:

/**
* roundup_pow_of_two - round the given value up to nearest power of two
* @n - parameter
*
* round the given value up to the nearest power of two
* - the result is undefined when n == 0


\
 
 \ /
  Last update: 2015-12-03 17:01    [W:0.051 / U:0.056 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site