linux/log2.h: Fix roundup_pow_of_two(0)
PATCH 1/1 linux/log2.h: Fix roundup_pow_of_two(0)
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).
--- include/linux/log2.h | 3 +++ 1 file changed, 3 insertions(+)
include/linux/log2.h +3 -0
@@ -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 ; +return 1UL <<fls_long (n -1 ); }
Signature · 1 line
1.7.10.4
2015-12-03 18:30 GMT+03:00 Sasha Levin <[email protected]>:
Quoted 16 lines
Passing 0 to
roundup_pow_of_twowould 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 <[email protected]>--- include/linux/log2.h | 3 +++ 1 file changed, 3 insertions(+)include/linux/log2.h +0
-0
include/linux/log2.h bool is_power_of_2(unsigned long n) · line 65
Quoted 6 lines
include/linux/log2.h +3
-0@@ -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
Quoted 12 lines · Andrey Ryabinin
2015-12-03 18:30 GMT+03:00 Sasha Levin <[email protected]>:
Quoted 11 lines
Quoted 11 lines
Passing 0 to
roundup_pow_of_twowould 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 <[email protected]>--- include/linux/log2.h | 3 +++ 1 file changed, 3 insertions(+)
Quoted 13 lines
Quoted 11 lines
include/linux/log2.h bool is_power_of_2(unsigned long n) · line 65
Quoted 11 lines
include/linux/log2.h +3
-0@@ -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 nearroundup_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
2015-12-03 19:04 GMT+03:00 Sasha Levin <[email protected]>:
Quoted 27 lines · Andrey Ryabinin
Quoted 12 lines
2015-12-03 18:30 GMT+03:00 Sasha Levin <[email protected]>:
Quoted 11 lines
Quoted 11 lines
Passing 0 to
roundup_pow_of_twowould 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 <[email protected]>--- include/linux/log2.h | 3 +++ 1 file changed, 3 insertions(+)Quoted 13 lines
Quoted 11 lines
include/linux/log2.h bool is_power_of_2(unsigned long n) · line 65
Quoted 11 lines
include/linux/log2.h +3
-0@@ -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 nearroundup_pow_of_two()says that result is undefined when n == 0:
Quoted 5 lines
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.
In that case patch should update the comment.
Quoted 2 lines
Thanks,
Sasha