macro: use __builtin_unreachable on NDEBUG

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
This commit is contained in:
NRK
2023-10-02 19:25:00 +06:00
committed by Daan De Meyer
parent db5d86f5b9
commit be1666886b
2 changed files with 2 additions and 2 deletions

View 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

View 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__); })