]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
macro: use __builtin_unreachable on NDEBUG
authorNRK <nrk@disroot.org>
Mon, 2 Oct 2023 13:25:00 +0000 (19:25 +0600)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 3 Oct 2023 19:34:38 +0000 (21:34 +0200)
note that this slightly changes the semantic of assert when NDEBUG is
defined. if there's an extern function call (without attribute pure or
similar) then the compiler has to assume it has side effects and still
emit the function call.

whereas the old assert guaranteed that nothing will be evaluated on
NDEBUG.

Closes: https://github.com/systemd/systemd/issues/29408
src/basic/macro.h
src/fundamental/macro-fundamental.h

index 3fb561412a4f2fe2eed604a2babf816537aa444e..0c99d68db2633c5644db6b4d8ab260be24de0e8c 100644 (file)
@@ -189,7 +189,7 @@ static inline int __coverity_check_and_return__(int condition) {
 /* We override the glibc assert() here. */
 #undef assert
 #ifdef NDEBUG
-#define assert(expr) do {} while (false)
+#define assert(expr) ({ if (!(expr)) __builtin_unreachable(); })
 #else
 #define assert(expr) assert_message_se(expr, #expr)
 #endif
index 02412705609828fca7d04283a6633e27cc6f263f..8063f020f832fe79ca2761d836d3eca3ced42fcc 100644 (file)
@@ -72,7 +72,7 @@
         _noreturn_ void efi_assert(const char *expr, const char *file, unsigned line, const char *function);
 
         #ifdef NDEBUG
-                #define assert(expr)
+                #define assert(expr) ({ if (!(expr)) __builtin_unreachable(); })
                 #define assert_not_reached() __builtin_unreachable()
         #else
                 #define assert(expr) ({ _likely_(expr) ? VOID_0 : efi_assert(#expr, __FILE__, __LINE__, __func__); })