test-tool {dump,scrap}-cache-tree: fix memory leaks

Fix memory leaks in two test-tools used by t0090-cache-tree.sh. As a result we can mark the 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 9afa46d4a677c806f414f363daee07a9e6a44f0b
3 files changed +9 -1
t/helper/test-dump-cache-tree.c
+6 -1
@@ -59,11 +59,16 @@ int cmd__dump_cache_tree(int ac, const char **av)
59 {
60 struct index_state istate;
61 struct cache_tree *another = cache_tree();
62 + int ret;
63 +
64 setup_git_directory();
65 if (read_cache() < 0)
66 die("unable to read index file");
67 istate = the_index;
68 istate.cache_tree = another;
69 cache_tree_update(&istate, WRITE_TREE_DRY_RUN);
68 - return dump_cache_tree(active_cache_tree, another, "");
70 + ret = dump_cache_tree(active_cache_tree, another, "");
71 + cache_tree_free(&another);
72 +
73 + return ret;
74 }
t/helper/test-scrap-cache-tree.c
+1
@@ -12,6 +12,7 @@ int cmd__scrap_cache_tree(int ac, const char **av)
12 hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
13 if (read_cache() < 0)
14 die("unable to read index file");
15 + cache_tree_free(&active_cache_tree);
16 active_cache_tree = NULL;
17 if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
18 die("unable to write index file");
t/t0090-cache-tree.sh
+2
@@ -5,6 +5,8 @@ test_description="Test whether cache-tree is properly updated
5 Tests whether various commands properly update and/or rewrite the
6 cache-tree extension.
7 "
8 +
9 +TEST_PASSES_SANITIZE_LEAK=true
10 . ./test-lib.sh
11
12 cmp_cache_tree () {