Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Wed, 19 Apr 2023 14:17:04 +0200
From: Jens Gustedt <>
Subject: [C23 divers headers 12/17] C23: change the assert macro to ...

Macro arguments are ambiguate fellows. A single C expression such as
(struct toto){ 1, 2}.a appears as two macro arguments.

This mandatory change has the macro accept an unlimited number of
macro-parameters, but which then must resolve to a single C
expression. This is achieved by squeezing the argument list through a
trivial function call to a static function.  By that we ensure that
lists that represent more than one expression (such as `1, 2`) and
empty lists are diagnosed.
 include/assert.h | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/include/assert.h b/include/assert.h
index 71007a10..034c4aa7 100644
--- a/include/assert.h
+++ b/include/assert.h
@@ -2,10 +2,17 @@
 #undef assert
+// Ensure that this version only accepts arguments that resolve to one
+// C expression after preprocessing.
+#define __ASSERT_EXPRESSION(...) __assert_expression(__VA_ARGS__)
+static inline _Bool __assert_expression(_Bool __x) { return __x; }
 #ifdef NDEBUG
-#define	assert(x) (void)0
+#define	assert(...) (void)0
-#define assert(x) ((void)((x) || (__assert_fail(#x, __FILE__, __LINE__, __func__),0)))
+#define assert(...) ((void)(__ASSERT_EXPRESSION(__VA_ARGS__) || (__assert_fail(#__VA_ARGS__, __FILE__, __LINE__, __func__),0)))
 #if __STDC_VERSION__ >= 201112L && __STDC_VERSION__ < 202311L && !defined(__cplusplus)

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.