]> git.ipfire.org Git - thirdparty/git.git/commitdiff
test-tool json-writer: fix memory leaks
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Fri, 1 Jul 2022 10:37:37 +0000 (12:37 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 1 Jul 2022 20:38:50 +0000 (13:38 -0700)
Fix memory leaks introduced with these tests in
75459410edd (json_writer: new routines to create JSON data,
2018-07-13), as a result we can mark a test as passing with
SANITIZE=leak using "TEST_PASSES_SANITIZE_LEAK=true".

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/helper/test-json-writer.c
t/t0019-json-writer.sh

index 37c452535f8bb2bcb3ee27461097ef0bf578ebac..8c3edacc0007df7ddb52f2f65d00e44b95edeb5a 100644 (file)
@@ -181,12 +181,18 @@ static struct json_writer nest1 = JSON_WRITER_INIT;
 
 static void make_nest1(int pretty)
 {
+       make_obj1(0);
+       make_arr1(0);
+
        jw_object_begin(&nest1, pretty);
        {
                jw_object_sub_jw(&nest1, "obj1", &obj1);
                jw_object_sub_jw(&nest1, "arr1", &arr1);
        }
        jw_end(&nest1);
+
+       jw_release(&obj1);
+       jw_release(&arr1);
 }
 
 static char *expect_inline1 =
@@ -313,6 +319,9 @@ static void make_mixed1(int pretty)
                jw_object_sub_jw(&mixed1, "arr1", &arr1);
        }
        jw_end(&mixed1);
+
+       jw_release(&obj1);
+       jw_release(&arr1);
 }
 
 static void cmp(const char *test, const struct json_writer *jw, const char *exp)
@@ -325,8 +334,8 @@ static void cmp(const char *test, const struct json_writer *jw, const char *exp)
        exit(1);
 }
 
-#define t(v) do { make_##v(0); cmp(#v, &v, expect_##v); } while (0)
-#define p(v) do { make_##v(1); cmp(#v, &v, pretty_##v); } while (0)
+#define t(v) do { make_##v(0); cmp(#v, &v, expect_##v); jw_release(&v); } while (0)
+#define p(v) do { make_##v(1); cmp(#v, &v, pretty_##v); jw_release(&v); } while (0)
 
 /*
  * Run some basic regression tests with some known patterns.
@@ -381,7 +390,6 @@ static int unit_tests(void)
 
        /* mixed forms */
        t(mixed1);
-       jw_init(&mixed1);
        p(mixed1);
 
        return 0;
@@ -544,7 +552,7 @@ static int scripted(void)
 
        printf("%s\n", jw.json.buf);
 
-       strbuf_release(&jw.json);
+       jw_release(&jw);
        return 0;
 }
 
index 3b0c336b38e4c7f81268227984472cb35572cdc2..19a730c29ed8f791d7885c31ff2a908cb1b5dfb3 100755 (executable)
@@ -1,6 +1,8 @@
 #!/bin/sh
 
 test_description='test json-writer JSON generation'
+
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'unit test of json-writer routines' '