]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: handle NULL correctly when passed to ASSERT_EQ()
authorFrantisek Sumsal <frantisek@sumsal.cz>
Mon, 8 Apr 2024 18:32:10 +0000 (20:32 +0200)
committerFrantisek Sumsal <frantisek@sumsal.cz>
Tue, 9 Apr 2024 19:39:46 +0000 (21:39 +0200)
strcmp() doesn't handle NULLs nicely, so switch to streq_ptr().

src/shared/tests.h

index 85f9463f9b5c5f1a2bcc69c4e6d8eabe4c2244a7..a8fa4a6622e7176083681e42f7710e2ccca1fb14 100644 (file)
@@ -249,11 +249,10 @@ static inline int run_test_table(void) {
 
 #define ASSERT_STREQ(expr1, expr2)                                                                              \
         ({                                                                                                      \
-                const char* _expr1 = (expr1);                                                                   \
-                const char* _expr2 = (expr2);                                                                   \
-                if (strcmp(_expr1, _expr2) != 0) {                                                              \
+                const char *_expr1 = (expr1), *_expr2 = (expr2);                                                \
+                if (!streq_ptr(_expr1, _expr2)) {                                                               \
                         log_error("%s:%i: Assertion failed: expected \"%s == %s\", but \"%s != %s\"",           \
-                                  PROJECT_FILE, __LINE__, #expr1, #expr2, _expr1, _expr2);                      \
+                                  PROJECT_FILE, __LINE__, #expr1, #expr2, strnull(_expr1), strnull(_expr2));    \
                         abort();                                                                                \
                 }                                                                                               \
         })