Skip to content

Commit 243c90e

Browse files
vincent-mailholYuryNorov
authored andcommitted
build_bug.h: more user friendly error messages in BUILD_BUG_ON_ZERO()
__BUILD_BUG_ON_ZERO_MSG(), as introduced in [1], makes it possible to do a static assertions in expressions. The direct benefit is to provide a meaningful error message instead of the cryptic negative bitfield size error message currently returned by BUILD_BUG_ON_ZERO(): ./include/linux/build_bug.h:16:51: error: negative width in bit-field '<anonymous>' 16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); }))) | ^ Get rid of BUILD_BUG_ON_ZERO()'s bitfield size hack. Instead rely on __BUILD_BUG_ON_ZERO_MSG() which in turn relies on C11's _Static_assert(). Use some macro magic, similarly to static_assert(), to either use an optional error message provided by the user or, when omitted, to produce a default error message by stringifying the tested expression. With this, for example: BUILD_BUG_ON_ZERO(1 > 0) would now throw: ./include/linux/compiler.h:197:62: error: static assertion failed: "1 > 0 is true" 197 | define __BUILD_BUG_ON_ZERO_MSG(e, msg) ((int)sizeof(struct {_Static_assert(!(e), msg);})) | ^~~~~~~~~~~~~~ Finally, __BUILD_BUG_ON_ZERO_MSG() is already guarded by an: #ifdef __CHECKER__ So no need any more for that guard clause for BUILD_BUG_ON_ZERO(). Remove it. [1] commit d7a516c ("compiler.h: Fix undefined BUILD_BUG_ON_ZERO()") Link: https://git.kernel.org/torvalds/c/d7a516c6eeae Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr> Link: https://git.kernel.org/next/linux-next/c/b88937277df Reviewed-by: Kees Cook <kees@kernel.org> Signed-off-by: Yury Norov <yury.norov@gmail.com>
1 parent e289b48 commit 243c90e

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

include/linux/build_bug.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

55
#include <linux/compiler.h>
66

7-
#ifdef __CHECKER__
8-
#define BUILD_BUG_ON_ZERO(e) (0)
9-
#else /* __CHECKER__ */
107
/*
118
* Force a compilation error if condition is true, but also produce a
129
* result (of value 0 and type int), so the expression can be used
1310
* e.g. in a structure initializer (or where-ever else comma expressions
1411
* aren't permitted).
12+
*
13+
* Take an error message as an optional second argument. If omitted,
14+
* default to the stringification of the tested expression.
1515
*/
16-
#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
17-
#endif /* __CHECKER__ */
16+
#define BUILD_BUG_ON_ZERO(e, ...) \
17+
__BUILD_BUG_ON_ZERO_MSG(e, ##__VA_ARGS__, #e " is true")
1818

1919
/* Force a compilation error if a constant expression is not a power of 2 */
2020
#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \

include/linux/compiler.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
192192
})
193193

194194
#ifdef __CHECKER__
195-
#define __BUILD_BUG_ON_ZERO_MSG(e, msg) (0)
195+
#define __BUILD_BUG_ON_ZERO_MSG(e, msg, ...) (0)
196196
#else /* __CHECKER__ */
197-
#define __BUILD_BUG_ON_ZERO_MSG(e, msg) ((int)sizeof(struct {_Static_assert(!(e), msg);}))
197+
#define __BUILD_BUG_ON_ZERO_MSG(e, msg, ...) ((int)sizeof(struct {_Static_assert(!(e), msg);}))
198198
#endif /* __CHECKER__ */
199199

200200
/* &a[0] degrades to a pointer: a different type from an array */

0 commit comments

Comments
 (0)