chiark / gitweb /
basic: nicer assert messages
authorMichal Schmidt <mschmidt@redhat.com>
Mon, 14 Sep 2015 13:53:36 +0000 (15:53 +0200)
committerSven Eden <yamakuzure@gmx.net>
Wed, 29 Mar 2017 08:45:09 +0000 (10:45 +0200)
Make sure the assert expression is not macro-expanded before
stringification. This makes several assertion failure messages more
readable.

As an example:
  assert(streq("foo", "bar"));

I'd rather see this:

Assertion 'streq("foo", "bar")' failed at foo.c:5, function main(). Aborting.

...than this, though awesome, incomprehensible truncated mess:

Assertion '(__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p ((
"foo")) && __builtin_constant_p (("bar")) && (__s1_len = strlen (("foo")), __s2_
len = strlen (("bar")), (!((size_t)(const void *)((("foo")) + 1) - (size_t)(cons
t void *)(("foo")) == 1) || __s1_len >= 4) && (!((size_t)(const void *)((("bar")
) + 1) - (size_t)(const void *)(("bar")) == 1) || __s2_len >= 4)) ? __builtin_st
rcmp (("foo"), ("bar")) : (__builtin_constant_p (("foo")) && ((size_t)(const voi
d *)((("foo")) + 1) - (size_t)(const void *)(("foo")) == 1) && (__s1_len = strle
n (("foo")), __s1_len < 4) ? (__builtin_constant_p (("bar")) && ((size_t)(const
void *)((("bar")) + 1) - (size_t)(const void *)(("bar")) == 1) ? __builtin_strcm
p (("foo"), ("bar")) : (__extension__ ({ const unsigned char *__s2 = (const unsi
gned char *) (const char *) (("bar")); int __result = (((const unsigned char *)
(const char *) (("foo")))[0] - __s2[0]); if (__s1_len > 0 && __result == 0) { __
result = (((const unsigned char *) (const char *) (("foo")))[1] - __s2[1]); if (
__s1_len > 1 && __result == 0) { __result = (((const unsigned char *) (const cha
r *) (("foo")))[2] - __s2[2]); if (__s1_len > 2 && __result == 0) __result = (((
const unsigned char *) (const char *) (("foo")))[3] - __s2[3]); } } __result; })
)) : (__builtin_constant_p (("bar")) && ((size_t)(const void *)((("bar")) + 1) -
 (size_t)(const void *)(("bar")) == 1) && (__s2_len = strlen (("bar")), __s2_len
 < 4) ? (__builtin_constant_p (("foo")) && ((size_t)(const void *)((("foo")) + 1
) - (size_t)(const void *)(("foo")) == 1) ? __builtin_strcmp (("foo"), ("bar"))
: (- (__extension__ ({ const unsigned char *__s2 = (const unsigned char *) (cons
t char *) (("foo")); int __result = (((const unsigned char *) (const char *) (("
bar")))[0] - __s2[0]); if (__s2_len > 0 && __result == 0) { __result = (((const
unsigned char *) (const char *) (("bar")))[1] - __s2[1]); if (__s2_len > 1 && __
result == 0) { __result = (((const unsigned char *) (const char *) (("bar")))[2]
 - __s2[2]); if (__s2_len > 2 && __result == 0)

src/basic/macro.h
src/libelogind/sd-bus/bus-internal.h

index 7715c6e691289d054534102d3018edb7c6e2b16e..b9a24f639ac45b71982fe3a0aac90c2be8e9b36d 100644 (file)
@@ -214,18 +214,20 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
                 (__x / __y + !!(__x % __y));                            \
         })
 
-#define assert_se(expr)                                                 \
+#define assert_message_se(expr, message)                                \
         do {                                                            \
                 if (_unlikely_(!(expr)))                                \
-                        log_assert_failed(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
-        } while (false)                                                 \
+                        log_assert_failed(message, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
+        } while (false)
+
+#define assert_se(expr) assert_message_se(expr, #expr)
 
 /* We override the glibc assert() here. */
 #undef assert
 #ifdef NDEBUG
 #define assert(expr) do {} while(false)
 #else
-#define assert(expr) assert_se(expr)
+#define assert(expr) assert_message_se(expr, #expr)
 #endif
 
 #define assert_not_reached(t)                                           \
@@ -250,19 +252,19 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
         REENABLE_WARNING
 #endif
 
-#define assert_log(expr) ((_likely_(expr))      \
-        ? (true)                                \
-        : (log_assert_failed_return(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__), false))
+#define assert_log(expr, message) ((_likely_(expr))                     \
+        ? (true)                                                        \
+        : (log_assert_failed_return(message, __FILE__, __LINE__, __PRETTY_FUNCTION__), false))
 
 #define assert_return(expr, r)                                          \
         do {                                                            \
-                if (!assert_log(expr))                                  \
+                if (!assert_log(expr, #expr))                           \
                         return (r);                                     \
         } while (false)
 
 #define assert_return_errno(expr, r, err)                               \
         do {                                                            \
-                if (!assert_log(expr)) {                                \
+                if (!assert_log(expr, #expr)) {                         \
                         errno = err;                                    \
                         return (r);                                     \
                 }                                                       \
index e0d7c9ee78b0c8f608ffdd55bf1f968eef8fb0f7..b4ec380cdc75a647db0f2e619692717288bf6dea 100644 (file)
@@ -396,6 +396,6 @@ int bus_maybe_reply_error(sd_bus_message *m, int r, sd_bus_error *error);
 
 #define bus_assert_return(expr, r, error)                               \
         do {                                                            \
-                if (!assert_log(expr))                                  \
+                if (!assert_log(expr, #expr))                           \
                         return sd_bus_error_set_errno(error, r);        \
         } while (false)