From ad741238823271c1dff20ab5785ed5d76c4960f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Mon, 4 Dec 2017 22:04:41 +0100 Subject: [PATCH] Handle null pointers in string comparisons --- src/kernel/include/ttest.h | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/kernel/include/ttest.h b/src/kernel/include/ttest.h index 1f79d9c..1af7e17 100644 --- a/src/kernel/include/ttest.h +++ b/src/kernel/include/ttest.h @@ -42,21 +42,28 @@ extern tt_test tt_tests[]; }while(0); #define ASSERT_STRN(lhs, rhs, n) do { \ + char *tt_lhs = (char *)(lhs); \ + char *tt_rhs = (char *)(rhs); \ + if(!tt_lhs || !tt_rhs) \ + { \ + TT_FAIL("Expected string, got null pointer\n", 0); \ + exit(1); \ + } \ size_t tt_n = (size_t)(n); \ - char *tt_lhs = malloc(tt_n+1); \ - char *tt_rhs = malloc(tt_n+1); \ - memcpy(tt_lhs, (lhs), tt_n); \ - memcpy(tt_rhs, (rhs), tt_n); \ - tt_rhs[tt_n] = tt_lhs[tt_n] = '\0'; \ - if(strncmp(tt_lhs, tt_rhs, tt_n)) { \ - TT_FAIL("Expected <%s> got <%s>\n", tt_rhs, tt_lhs); \ - free(tt_lhs); free(tt_rhs); \ + char *tt_lhs_c = malloc(tt_n+1); \ + char *tt_rhs_c = malloc(tt_n+1); \ + memcpy(tt_lhs_c, tt_lhs, tt_n); \ + memcpy(tt_rhs_c, tt_rhs, tt_n); \ + tt_rhs_c[tt_n] = tt_lhs_c[tt_n] = '\0'; \ + if(strncmp(tt_lhs_c, tt_rhs_c, tt_n)) { \ + TT_FAIL("Expected <%s> got <%s>\n", tt_rhs_c, tt_lhs_c); \ + free(tt_lhs_c); free(tt_rhs_c); \ exit(1); \ } \ - free(tt_lhs); free(tt_rhs); \ + free(tt_lhs_c); free(tt_rhs_c); \ }while(0); -#define TEST(name, body) void ttt_##name() { tt_current_test = #name; if(tt_before) tt_before(); body } +#define TEST(name, ...) void ttt_##name() { tt_current_test = #name; if(tt_before) tt_before(); __VA_ARGS__ } #define BEFORE(body) void tt_before() { body }