test-tool json-writer: fix memory leaks

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>

Ævar Arnfjörð Bjarmason committed Jul 1, 2022 at 12:37 UTC 1caaa858ccec11adaa3f9a649c15b766da474078
2 files changed +14 -4
t/helper/test-json-writer.c
+12 -4
@@ -181,12 +181,18 @@ static struct json_writer nest1 = JSON_WRITER_INIT;
181
182 static void make_nest1(int pretty)
183 {
184 + make_obj1(0);
185 + make_arr1(0);
186 +
187 jw_object_begin(&nest1, pretty);
188 {
189 jw_object_sub_jw(&nest1, "obj1", &obj1);
190 jw_object_sub_jw(&nest1, "arr1", &arr1);
191 }
192 jw_end(&nest1);
193 +
194 + jw_release(&obj1);
195 + jw_release(&arr1);
196 }
197
198 static char *expect_inline1 =
@@ -313,6 +319,9 @@ static void make_mixed1(int pretty)
319 jw_object_sub_jw(&mixed1, "arr1", &arr1);
320 }
321 jw_end(&mixed1);
322 +
323 + jw_release(&obj1);
324 + jw_release(&arr1);
325 }
326
327 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)
334 exit(1);
335 }
336
328 -#define t(v) do { make_##v(0); cmp(#v, &v, expect_##v); } while (0)
329 -#define p(v) do { make_##v(1); cmp(#v, &v, pretty_##v); } while (0)
337 +#define t(v) do { make_##v(0); cmp(#v, &v, expect_##v); jw_release(&v); } while (0)
338 +#define p(v) do { make_##v(1); cmp(#v, &v, pretty_##v); jw_release(&v); } while (0)
339
340 /*
341 * Run some basic regression tests with some known patterns.
@@ -381,7 +390,6 @@ static int unit_tests(void)
390
391 /* mixed forms */
392 t(mixed1);
384 - jw_init(&mixed1);
393 p(mixed1);
394
395 return 0;
@@ -544,7 +552,7 @@ static int scripted(void)
552
553 printf("%s\n", jw.json.buf);
554
547 - strbuf_release(&jw.json);
555 + jw_release(&jw);
556 return 0;
557 }
558
t/t0019-json-writer.sh
+2
@@ -1,6 +1,8 @@
1 #!/bin/sh
2
3 test_description='test json-writer JSON generation'
4 +
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 test_expect_success 'unit test of json-writer routines' '