From 4facafcf702ec54694451d5bdda4f1faff94718d Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 4 Apr 2022 13:03:39 -0500 Subject: fix memory leak in _lily_assert_msg --- lily-test.c | 6 ++---- tests/assertions.c | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lily-test.c b/lily-test.c index 7484894..17959ca 100644 --- a/lily-test.c +++ b/lily-test.c @@ -30,7 +30,8 @@ static void _lily_assert_msg(bool statement, const char *location, size_t length = vsnprintf(NULL, 0, format_string, args_len); va_end(args_len); - if (_lily_globals.error_msg_len < length+1) { + if (_lily_globals.error_msg_len < length+1 || + _lily_globals.error_msg == NULL) { if (_lily_globals.error_msg != NULL) free(_lily_globals.error_msg); @@ -46,12 +47,9 @@ static void _lily_assert_msg(bool statement, const char *location, else { _lily_globals.error_msg = msg; _lily_globals.error_msg_len = length+1; - printf("reallocated global msg: %p ", msg); } } - printf("length: %lu ", length); - vsnprintf(_lily_globals.error_msg, length+1, format_string, args); va_end(args); diff --git a/tests/assertions.c b/tests/assertions.c index 6471225..fa7e82d 100644 --- a/tests/assertions.c +++ b/tests/assertions.c @@ -1,4 +1,5 @@ #include +#include #include #include "lily-test.h" @@ -8,7 +9,7 @@ const char *test_LILY_LOCATION() { // if you move this line, you MUST update the expected string! const char *location = LILY_LOCATION; - int diff = strcmp(location, "tests/assertions.c:10"); + int diff = strcmp(location, "tests/assertions.c:11"); if (diff != 0) return "LILY_LOCATION did not resolve correctly!"; @@ -39,10 +40,11 @@ const char *test_assert_msg() else { if (strcmp(_lily_globals.error_msg, "should fail!") != 0) return "false assertion produced incorrect error message!"; - if (strcmp(_lily_globals.error_location, "tests/assertions.c:36")) + if (strcmp(_lily_globals.error_location, "tests/assertions.c:37")) return "false assertion produced incorrect error location!"; } + free(_lily_globals.error_msg); return 0; } @@ -71,6 +73,7 @@ const char *test_assert_true() return "false assertion produced incorrect error message!"; } + free(_lily_globals.error_msg); return 0; } @@ -98,6 +101,7 @@ const char *test_assert_false() return "false assertion produced incorrect error message!"; } + free(_lily_globals.error_msg); return 0; } @@ -130,6 +134,7 @@ const char *test_assert_not_null() return "false assertion produced incorrect error message!"; } + free(_lily_globals.error_msg); return 0; } @@ -162,6 +167,7 @@ const char *test_assert_null() return "false assertion produced incorrect error message!"; } + free(_lily_globals.error_msg); return 0; } @@ -196,6 +202,7 @@ const char *test_assert_ptr_equal() return "false assertion produced incorrect error message!"; } + free(_lily_globals.error_msg); return 0; } @@ -230,6 +237,7 @@ const char *test_assert_ptr_not_equal() return "false assertion produced incorrect error message!"; } + free(_lily_globals.error_msg); return 0; } @@ -262,6 +270,7 @@ const char *test_assert_int_equal() return "false assertion produced incorrect error message!"; } + free(_lily_globals.error_msg); return 0; } @@ -293,5 +302,6 @@ const char *test_assert_int_not_equal() return "false assertion produced incorrect error message!"; } + free(_lily_globals.error_msg); return 0; } -- cgit v1.2.1