All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] linux/log2.h: Fix roundup_pow_of_two(0)
@ 2015-12-03 15:30 Sasha Levin
  2015-12-03 15:39 ` Andrey Ryabinin
  0 siblings, 1 reply; 4+ messages in thread
From: Sasha Levin @ 2015-12-03 15:30 UTC (permalink / raw)
  To: torvalds; +Cc: linux-kernel, Sasha Levin

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;
+
 	return 1UL << fls_long(n - 1);
 }
 
-- 
1.7.10.4


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-12-03 16:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2015-12-03 16:15     ` Andrey Ryabinin

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.