]> git.ipfire.org Git - pakfire.git/commitdiff
tests: Avoid leaking memory in parser tests
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 22 Feb 2025 17:46:25 +0000 (17:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 22 Feb 2025 17:46:25 +0000 (17:46 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/libpakfire/parser.c
tests/testsuite.h

index 618ed2ea746a7d155e761ff17c37820684747647..5eb434e4a322fed3a9d253dacda2258ef67b359f 100644 (file)
 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;
index c6bbe71e4d504027f4fb667bb5893eb21bfbd3dc..d9a0ae7b87b8a405672574af52fffdd72ed14e47 100644 (file)
@@ -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)) { \