std-aux: mark failures of nm_assert() as unreachable code

- with nm_assert(), if the argument is a compile time constant
  always check it (regardless of NDEBUG, G_DISABLE_ASSERT)
  and mark the failure as _nm_unreachable_code(). We do this,
  even if we usually would not evaluate run time checks with
  NDEBUG/G_DISABLE_ASSERT.

- with nm_assert_se(), if assertions are disabled with NDEBUG
  and G_DISABLE_ASSERT, still mark the path as _nm_unreachable_code().
This commit is contained in:
Thomas Haller 2022-11-30 10:01:17 +01:00
parent 06931221b5
commit 4753358dd5
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -253,21 +253,26 @@ _nm_assert_fail_internal(const char *assertion,
#define NM_MORE_ASSERTS_EFFECTIVE (_NM_ASSERT_FAIL_ENABLED ? NM_MORE_ASSERTS : 0)
#define nm_assert(cond) \
({ \
#define nm_assert(cond) \
({ \
/* nm_assert() must do *nothing* of effect, except evaluating
* @cond (0 or 1 times).
*
* As such, nm_assert() is async-signal-safe (provided @cond is, and
* the assertion does not fail). */ \
if (NM_MORE_ASSERTS_EFFECTIVE == 0) { \
/* pass */ \
} else if (NM_LIKELY(cond)) { \
/* pass */ \
} else { \
_nm_assert_fail(#cond); \
} \
1; \
* the assertion does not fail). */ \
if (NM_MORE_ASSERTS_EFFECTIVE == 0) { \
if (__builtin_constant_p(cond) && !(cond)) { \
/* Constant expressions are still evaluated and result
* in unreachable code. This handles nm_assert(FALSE). */ \
_nm_unreachable_code(); \
} \
/* pass */ \
} else if (NM_LIKELY(cond)) { \
/* pass */ \
} else { \
_nm_assert_fail(#cond); \
} \
1; \
})
#define nm_assert_se(cond) \
@ -279,10 +284,11 @@ _nm_assert_fail_internal(const char *assertion,
* the assertion does not fail). */ \
if (NM_LIKELY(cond)) { \
/* pass */ \
} else if (NM_MORE_ASSERTS_EFFECTIVE == 0) { \
/* pass */ \
} else { \
_nm_assert_fail(#cond); \
if (NM_MORE_ASSERTS_EFFECTIVE != 0) { \
_nm_assert_fail(#cond); \
} \
_nm_unreachable_code(); \
} \
1; \
})