std-aux: drop assertion and function name from assert() in release mode
For g_assert() and g_return*() we already do the same, in "src/libnm-glib-aux/nm-gassert-patch.h" Also patch __assert_fail() so that it omits the condition text and the function name in production builds. Note that this is a bit ugly, for two reasons: - again, we make assumptions that __assert_fail() exists. In practice, this is the case for glibc and musl. - <assert.h> can be included multiple times, while also forward declaring __assert_fail(). That means, we cannot add a macro #define __assert_fail(...) because that would break the forward declaration. Instead, just `#define __assert_fail _nm_assert_fail_internal` Of course, this only affects direct calls to assert(), which we have few. nm_assert() is not affected, because that anyway doesn't do anything, unless NM_MORE_ASSERTS is enabled.
This commit is contained in:
@@ -78,6 +78,9 @@ test_gpid(void)
|
|||||||
* the case. */
|
* the case. */
|
||||||
int_ptr = &pid;
|
int_ptr = &pid;
|
||||||
g_assert_cmpint(*int_ptr, ==, 42);
|
g_assert_cmpint(*int_ptr, ==, 42);
|
||||||
|
|
||||||
|
/* also check how assert() works. */
|
||||||
|
assert(*int_ptr == 42);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
|
@@ -194,6 +194,25 @@ typedef uint64_t _nm_bitwise nm_be64_t;
|
|||||||
#define NM_MORE_ASSERTS 0
|
#define NM_MORE_ASSERTS 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if NM_MORE_ASSERTS == 0
|
||||||
|
/* The string with the assertion check and the function name blows up the
|
||||||
|
* binary size. In production mode, let's drop those, similar to
|
||||||
|
* g_assertion_message_expr.
|
||||||
|
*
|
||||||
|
* Note that <assert.h> can be included multiple times. We can thus
|
||||||
|
* not redefine __assert_fail(...). Instead, just redefine the name
|
||||||
|
* __assert_fail. */
|
||||||
|
_nm_noreturn static inline void
|
||||||
|
_nm_assert_fail_internal(const char *assertion,
|
||||||
|
const char *file,
|
||||||
|
unsigned int line,
|
||||||
|
const char *function)
|
||||||
|
{
|
||||||
|
__assert_fail("<dropped>", file, line, "<unknown-fcn>");
|
||||||
|
}
|
||||||
|
#define __assert_fail _nm_assert_fail_internal
|
||||||
|
#endif
|
||||||
|
|
||||||
#define _nm_assert_fail(msg) __assert_fail((msg), __FILE__, __LINE__, __func__)
|
#define _nm_assert_fail(msg) __assert_fail((msg), __FILE__, __LINE__, __func__)
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
#define _NM_ASSERT_FAIL_ENABLED 1
|
#define _NM_ASSERT_FAIL_ENABLED 1
|
||||||
|
Reference in New Issue
Block a user