From 29fd258d4174592d9c6cad2e45d826547727ff02 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 22 Feb 2025 17:46:25 +0000 Subject: [PATCH] tests: Avoid leaking memory in parser tests Signed-off-by: Michael Tremer --- tests/libpakfire/parser.c | 43 +++++++++++++++++---------------------- tests/testsuite.h | 13 ++++++++++++ 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/tests/libpakfire/parser.c b/tests/libpakfire/parser.c index 618ed2ea7..5eb434e4a 100644 --- a/tests/libpakfire/parser.c +++ b/tests/libpakfire/parser.c @@ -31,48 +31,42 @@ static int test_parser(const struct test* t) { struct pakfire_parser* parser = NULL; struct pakfire_parser* subparser = NULL; - char* value = NULL; int r = EXIT_FAILURE; + char* s = NULL; // Create a new parser ASSERT_SUCCESS(pakfire_parser_create(&parser, t->pakfire, NULL, NULL, 0)); // Retrieve a value that does not exist - value = pakfire_parser_get(parser, NULL, "null"); - ASSERT(!value); + ASSERT_NULL(pakfire_parser_get(parser, NULL, "null")); // Set a value ASSERT_SUCCESS(pakfire_parser_set(parser, NULL, "a", "a", 0)); // Retrieve the value again - value = pakfire_parser_get(parser, NULL, "a"); - ASSERT_STRING_EQUALS(value, "a"); + ASSERT_STRING_EQUALS_FREE(pakfire_parser_get(parser, NULL, "a"), "a"); // Append something to the value ASSERT_SUCCESS(pakfire_parser_append(parser, NULL, "a", "b")); // Retrieve the value again - value = pakfire_parser_get(parser, NULL, "a"); - ASSERT_STRING_EQUALS(value, "a b"); + ASSERT_STRING_EQUALS_FREE(pakfire_parser_get(parser, NULL, "a"), "a b"); // Make a child parser subparser = pakfire_parser_create_child(parser, "child"); ASSERT(subparser); // Try to get a again - value = pakfire_parser_get(subparser, NULL, "a"); - ASSERT_STRING_EQUALS(value, "a b"); + ASSERT_STRING_EQUALS_FREE(pakfire_parser_get(subparser, NULL, "a"), "a b"); // Append something to the subparser ASSERT_SUCCESS(pakfire_parser_append(subparser, NULL, "a", "c")); // The subparser should return "a b c" - value = pakfire_parser_get(subparser, NULL, "a"); - ASSERT_STRING_EQUALS(value, "a b c"); + ASSERT_STRING_EQUALS_FREE(pakfire_parser_get(subparser, NULL, "a"), "a b c"); // The original parser should remain unchanged - value = pakfire_parser_get(parser, NULL, "a"); - ASSERT_STRING_EQUALS(value, "a b"); + ASSERT_STRING_EQUALS_FREE(pakfire_parser_get(parser, NULL, "a"), "a b"); // Set another value ASSERT_SUCCESS(pakfire_parser_append(subparser, NULL, "b", "1")); @@ -84,11 +78,10 @@ static int test_parser(const struct test* t) { ASSERT_SUCCESS(pakfire_parser_set(parser, NULL, "c", "%{b}", 0)); // Get the value of c - value = pakfire_parser_get(parser, NULL, "c"); - ASSERT_STRING_EQUALS(value, ""); + ASSERT_STRING_EQUALS_FREE(pakfire_parser_get(parser, NULL, "c"), ""); // Dump the parser - char* s = pakfire_parser_dump(parser); + s = pakfire_parser_dump(parser); printf("%s\n", s); // Everything passed @@ -99,8 +92,8 @@ FAIL: pakfire_parser_unref(subparser); if (parser) pakfire_parser_unref(parser); - if (value) - free(value); + if (s) + free(s); return r; } @@ -154,6 +147,7 @@ FAIL: static int test_append(const struct test* t) { struct pakfire_parser* parser = NULL; int r = EXIT_FAILURE; + char* s = NULL; // Create a new parser ASSERT_SUCCESS(pakfire_parser_create(&parser, t->pakfire, NULL, NULL, 0)); @@ -183,12 +177,12 @@ static int test_append(const struct test* t) { ASSERT_SUCCESS(pakfire_parser_parse(parser, str2, strlen(str2), NULL)); - ASSERT_STRING_EQUALS(pakfire_parser_get(parser, "build", "a"), "1 2 3"); - ASSERT_STRING_EQUALS(pakfire_parser_get(parser, "build", "b"), "1 2 3"); - ASSERT_STRING_EQUALS(pakfire_parser_get(parser, "build", "c"), "10 20"); + ASSERT_STRING_EQUALS_FREE(pakfire_parser_get(parser, "build", "a"), "1 2 3"); + ASSERT_STRING_EQUALS_FREE(pakfire_parser_get(parser, "build", "b"), "1 2 3"); + ASSERT_STRING_EQUALS_FREE(pakfire_parser_get(parser, "build", "c"), "10 20"); // Dump the parser - char* s = pakfire_parser_dump(parser); + s = pakfire_parser_dump(parser); printf("%s\n", s); // Everything passed @@ -197,6 +191,8 @@ static int test_append(const struct test* t) { FAIL: if (parser) pakfire_parser_unref(parser); + if (s) + free(s); return r; } @@ -211,8 +207,7 @@ static int test_parser_command(const struct test* t) { ASSERT_SUCCESS(pakfire_parser_set(parser, NULL, "command", command, 0)); // Retrieve the expanded value - char* value = pakfire_parser_get(parser, NULL, "command"); - ASSERT_STRING_EQUALS(value, "ABC"); + ASSERT_STRING_EQUALS_FREE(pakfire_parser_get(parser, NULL, "command"), "ABC"); // Everything passed r = EXIT_SUCCESS; diff --git a/tests/testsuite.h b/tests/testsuite.h index c6bbe71e4..d9a0ae7b8 100644 --- a/tests/testsuite.h +++ b/tests/testsuite.h @@ -228,6 +228,19 @@ ERROR: } \ } while (0) +#define ASSERT_STRING_EQUALS_FREE(value, string) \ + do { \ + int __r = pakfire_string_equals(value, string); \ + if (!__r) { \ + LOG_ERROR("Failed assertion: " #value " (%s) != " #string " (%s) %s:%d %s\n", \ + value, string, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ + } \ + if (value) \ + free(value); \ + if (!__r) \ + goto FAIL; \ + } while (0) + #define ASSERT_STRING_ARRAY_EQUALS(array1, array2) \ do { \ if (testsuite_compare_string_arrays(array1, array2)) { \ -- 2.39.5