Skip to contentSkip to conversation

linux/log2.h: enclose macro arg in parens

PATCH Superseded Jonathan Lemon 2 participants 2 messages Jun 23, 2020

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux/log2.h: enclose macro arg in parens

From: Jonathan Lemon <[email protected]>

roundup_pow_of_two uses its arg without enclosing it in parens.

A call of the form:

roundup_pow_of_two(boolval ? PAGE_SIZE : frag_size)

resulted in an compile warning:

warning: ?: using integer constants in boolean context [-Wint-in-bool-context]
PAGE_SIZE :
../include/linux/log2.h:176:4: note: in definition of macro ‘roundup_pow_of_two’

   (n == 1) ? 1 :  \
    ^

And the resulting code used '1' as the result of the operation.

Fixes 312a0c170945 ("[PATCH] LOG2: Alter roundup_pow_of_two() so that it can use a ilog2() on a constant")
---
 include/linux/log2.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/log2.h +1 -1
index 83a4a3ca3e8a..c619ec6eff4a 100644
@@ -173,7 +173,7 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
 #define roundup_pow_of_two(n)			\
 (						\
 	__builtin_constant_p(n) ? (		\
-		(n == 1) ? 1 :			\
+		((n) == 1) ? 1 :		\
 		(1UL << (ilog2((n) - 1) + 1))	\
 				   ) :		\
 	__roundup_pow_of_two(n)			\
Signature · 1 line
2.24.1
maintainer
#1 lore raw
Quoted 8 lines · On Tue, 23 Jun 2020 10:07:39 -0700 Jonathan Lemon wrote:

From: Jonathan Lemon <[email protected]>

roundup_pow_of_two uses its arg without enclosing it in parens.

A call of the form:

roundup_pow_of_two(boolval ? PAGE_SIZE : frag_size)

Quoted 8 lines

resulted in an compile warning:

warning: ?: using integer constants in boolean context [-Wint-in-bool-context]
PAGE_SIZE :
../include/linux/log2.h:176:4: note: in definition of macro ‘roundup_pow_of_two’

   (n == 1) ? 1 :  \
    ^

And the resulting code used '1' as the result of the operation.

I'd have thought that this warning is harmless, since the expression clearly is not a constant, interesting.

Quoted 2 lines
Fixes 312a0c170945 ("[PATCH] LOG2: Alter roundup_pow_of_two() so that it can use a ilog2() on a constant")

Please no empty line between fixes and other tags.

Quoted 18 lines
---
 include/linux/log2.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/log2.h +1 -1
index 83a4a3ca3e8a..c619ec6eff4a 100644
@@ -173,7 +173,7 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
 #define roundup_pow_of_two(n)			\
 (						\
 	__builtin_constant_p(n) ? (		\
-		(n == 1) ? 1 :			\
+		((n) == 1) ? 1 :		\
 		(1UL << (ilog2((n) - 1) + 1))	\
 				   ) :		\
 	__roundup_pow_of_two(n)			\