mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] compiler.h: fix error in BUILD_BUG_ON() reporting
@ 2020-03-31 11:26 Vegard Nossum
  2020-03-31 17:31 ` Masahiro Yamada
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Vegard Nossum @ 2020-03-31 11:26 UTC (permalink / raw)
  To: Andrew Morton
  Cc: linux-kernel, Daniel Santos, Rasmus Villemoes, Masahiro Yamada,
	Ian Abbott

compiletime_assert() uses __LINE__ to create a unique function name.
This means that if you have more than one BUILD_BUG_ON() in the same
source line (which can happen if they appear e.g. in a macro), then
the error message from the compiler might output the wrong condition.

For this source file:

	#include <linux/build_bug.h>

	#define macro() \
		BUILD_BUG_ON(1); \
		BUILD_BUG_ON(0);

	void foo()
	{
		macro();
	}

gcc would output:

./include/linux/compiler.h:350:38: error: call to ‘__compiletime_assert_9’ declared with attribute error: BUILD_BUG_ON failed: 0
  _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)

However, it was not the BUILD_BUG_ON(0) that failed, so it should say 1
instead of 0. With this patch, we use __COUNTER__ instead of __LINE__, so
each BUILD_BUG_ON() gets a different function name and the correct
condition is printed:

./include/linux/compiler.h:350:38: error: call to ‘__compiletime_assert_0’ declared with attribute error: BUILD_BUG_ON failed: 1
  _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)

Cc: Daniel Santos <daniel.santos@pobox.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Masahiro Yamada <yamada.masahiro@socionext.com>
Cc: Ian Abbott <abbotti@mev.co.uk>
Signed-off-by: Vegard Nossum <vegard.nossum@oracle.com>
---
 include/linux/compiler.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index 5e88e7e33abec..034b0a644efcc 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -347,7 +347,7 @@ static inline void *offset_to_ptr(const int *off)
  * compiler has support to do so.
  */
 #define compiletime_assert(condition, msg) \
-	_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
+	_compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
 
 #define compiletime_assert_atomic_type(t)				\
 	compiletime_assert(__native_word(t),				\
-- 
2.16.1.72.g5be1f00a9.dirty


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

end of thread, other threads:[~2020-04-01  1:14 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-31 11:26 [PATCH] compiler.h: fix error in BUILD_BUG_ON() reporting Vegard Nossum
2020-03-31 17:31 ` Masahiro Yamada
2020-03-31 18:20 ` Joe Perches
2020-03-31 18:56   ` Rasmus Villemoes
2020-03-31 19:00     ` Joe Perches
2020-03-31 19:05       ` Rasmus Villemoes
2020-03-31 19:08       ` Vegard Nossum
2020-04-01  1:12         ` Daniel Santos
2020-03-31 22:30     ` Daniel Santos
2020-03-31 22:25 ` Daniel Santos
2020-03-31 22:36   ` Nathan Chancellor
2020-04-01  0:53     ` Daniel Santos

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®