diff options
author | sanine <sanine.not@pm.me> | 2023-02-04 11:23:37 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-02-04 11:23:37 -0600 |
commit | 7c93fe6bc9f061484d28115e9fa1ec5f89ce5924 (patch) | |
tree | 847a46f89fcb9b2e53c84e39a3e153f4dbd7b010 | |
parent | c7bf6fcccd50ded889b7cfb853af51e7c5f28d45 (diff) |
fix segfault when comparing NULL strings
-rw-r--r-- | lily-test.h | 26 | ||||
-rw-r--r-- | tests/assertions.c | 64 | ||||
-rw-r--r-- | tests/tests.h | 2 |
3 files changed, 89 insertions, 3 deletions
diff --git a/lily-test.h b/lily-test.h index e4f1676..a4dc00a 100644 --- a/lily-test.h +++ b/lily-test.h @@ -48,7 +48,7 @@ #define LILY_VERSION_MAJOR 2 #define LILY_VERSION_MINOR 0 -#define LILY_VERSION_PATCH 0 +#define LILY_VERSION_PATCH 1 #include <stdbool.h> #include <stddef.h> @@ -69,6 +69,9 @@ #define LILY_LOCATION ((__FILE__ ":" STR(__LINE__)) + SOURCE_PATH_SIZE) #endif +#define LILY_NULLSAFE(x) (x==NULL ? "(nil)" : x) +int lily_streq(const char *str1, const char *str2); + /* self-location macro */ #ifndef LILY_TEST_H_LOCATION @@ -161,7 +164,7 @@ void lily_check(int x, const char *location, const char *fmt, ...); #define CHECK_EQF(x, y, fmt) LILY_CHECK_EQF(x, y, #x, #y, fmt) #define LILY_CHECK_EQS(x, y, xstr, ystr) \ - lily_check(strcmp(x, y) == 0, LILY_LOCATION, \ + lily_check(lily_streq(x, y), LILY_LOCATION, \ "CHECK failed: %s == %s\n %s = \"%s\"\n %s = \"%s\"", \ xstr, ystr, xstr, x, ystr, y) #define CHECK_EQS(x, y) LILY_CHECK_EQS(x, y, #x, #y) @@ -194,7 +197,7 @@ void lily_require(int x, const char *location, const char *fmt, ...); #define REQUIRE_EQF(x, y, fmt) LILY_REQUIRE_EQF(x, y, #x, #y, fmt) #define LILY_REQUIRE_EQS(x, y, xstr, ystr) \ - lily_require(strcmp(x, y) == 0, LILY_LOCATION, \ + lily_require(lily_streq(x, y), LILY_LOCATION, \ "REQUIRE failed: %s == %s\n %s = \"%s\"\n %s = \"%s\"", \ xstr, ystr, xstr, x, ystr, y) #define REQUIRE_EQS(x, y) LILY_REQUIRE_EQS(x, y, #x, #y) @@ -368,6 +371,23 @@ void lily_set_epsilon(double epsilon) { lily_g.epsilon = epsilon; } + + +int lily_streq(const char *str1, const char *str2) +{ + if (str1 == NULL && str2 == NULL) { + /* both are null (and therefore equal, i guess) */ + return 1; + } + + if (str1 == NULL || str2 == NULL) { + /* only one is null */ + return 0; + } + + /* neither are null, check normal string equality */ + return strcmp(str1, str2) == 0; +} #endif /* ifdef LILY_TEST_H */ diff --git a/tests/assertions.c b/tests/assertions.c index 6951f58..280bb97 100644 --- a/tests/assertions.c +++ b/tests/assertions.c @@ -149,6 +149,33 @@ const char * test_CHECK_EQS() return 0; } +const char * test_CHECK_EQS_NULL() +{ + lily_g.HEAD.next = NULL; + lily_g.TAIL = &(lily_g.HEAD); + lily_g.failed = 0; + + const char *a = "hi"; + const char *b = "bye"; + CHECK_EQS(a, NULL); + CHECK_EQS(a, "hi"); + + if (lily_g.HEAD.next == NULL) + return "CHECK_EQS did not append any failure message"; + + if (lily_g.HEAD.next->next != NULL) + return "CHECK_EQS appended more than one message"; + + if (strcmp( + lily_g.HEAD.next->msg, + "CHECK failed: a == NULL\n a = \"hi\"\n NULL = \"(null)\"" + ) != 0) + return "incorrect message"; + + lily_msg_destroy(lily_g.HEAD.next); + return 0; +} + @@ -303,3 +330,40 @@ const char * test_REQUIRE_EQS() return "test continued to run!"; } } + +void f_test_REQUIRE_EQS_NULL() +{ + const char *a = "hi"; + const char *b = "bye"; + REQUIRE_EQS(a, NULL); + REQUIRE_EQS("hi hi"+3, a); +} +const char * test_REQUIRE_EQS_NULL() +{ + lily_g.HEAD.next = NULL; + lily_g.TAIL = &(lily_g.HEAD); + lily_g.failed = 0; + int test_failed = setjmp(lily_g.env); + + if (test_failed) { + if (lily_g.HEAD.next == NULL) + return "REQUIRE_EQS did not append any failure message"; + + if (lily_g.HEAD.next->next != NULL) + return "REQUIRE_EQS appended more than one message"; + + if (strcmp( + lily_g.HEAD.next->msg, + "REQUIRE failed: a == NULL\n a = \"hi\"\n NULL = \"(null)\"" + ) != 0) + return "incorrect message"; + + lily_msg_destroy(lily_g.HEAD.next); + return 0; + } + else { + f_test_REQUIRE_EQS_NULL(); + return "test continued to run!"; + } + +} diff --git a/tests/tests.h b/tests/tests.h index 4a02f66..448f14f 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -17,10 +17,12 @@ int validate_message(const char* received, const char* expected, X(test_CHECK_EQ) \ X(test_CHECK_EQF) \ X(test_CHECK_EQS) \ + X(test_CHECK_EQS_NULL) \ X(test_REQUIRE) \ X(test_REQUIRE_EQ) \ X(test_REQUIRE_EQF) \ X(test_REQUIRE_EQS) \ + X(test_REQUIRE_EQS_NULL) \ #define X(test) const char * test(); TESTS |