]> git.ipfire.org Git - thirdparty/git.git/commitdiff
match-trees: fix leaking prefixes in `shift_tree()`
authorPatrick Steinhardt <ps@pks.im>
Thu, 5 Sep 2024 10:09:36 +0000 (12:09 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 5 Sep 2024 15:49:12 +0000 (08:49 -0700)
In `shift_tree()` we allocate two empty strings that we end up
passing to `match_trees()`. If that function finds a better match it
will update these pointers to point to a newly allocated strings,
freeing the old strings. We never free the final results though, neither
the ones we have allocated ourselves, nor the one that `match_trees()`
might've returned to us.

Fix the resulting memory leaks by creating a common exit path where we
free them.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
match-trees.c
t/t6409-merge-subtree.sh

index f17c74d483f849f9773d4c9c7a7645d05a5e7680..147b03abf18f65a7c0a0f262960e4a4b740d27b7 100644 (file)
@@ -294,18 +294,22 @@ void shift_tree(struct repository *r,
                unsigned short mode;
 
                if (!*del_prefix)
-                       return;
+                       goto out;
 
                if (get_tree_entry(r, hash2, del_prefix, shifted, &mode))
                        die("cannot find path %s in tree %s",
                            del_prefix, oid_to_hex(hash2));
-               return;
+               goto out;
        }
 
        if (!*add_prefix)
-               return;
+               goto out;
 
        splice_tree(hash1, add_prefix, hash2, shifted);
+
+out:
+       free(add_prefix);
+       free(del_prefix);
 }
 
 /*
index e9ba6f1690d015d06e13c69db2c1190ef8436443..528615b981f899f1b02c3ab96bc6c794724700be 100755 (executable)
@@ -5,6 +5,7 @@ test_description='subtree merge strategy'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success setup '