match-trees: fix leaking prefixes in `shift_tree()`
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>
Patrick Steinhardt committed
Sep 5, 2024 at 12:09 UTC
2a0147089151e3dd2cc8e6c91ecaf45fb416630e
2 files changed
+8
-3
match-trees.c
+7
-3
@@ -294,18 +294,22 @@ void shift_tree(struct repository *r,
294
unsigned short mode;
295
296
if (!*del_prefix)
297
- return;
297
+ goto out;
298
299
if (get_tree_entry(r, hash2, del_prefix, shifted, &mode))
300
die("cannot find path %s in tree %s",
301
del_prefix, oid_to_hex(hash2));
302
- return;
302
+ goto out;
303
}
304
305
if (!*add_prefix)
306
- return;
306
+ goto out;
307
308
splice_tree(hash1, add_prefix, hash2, shifted);
309
+
310
+out:
311
+ free(add_prefix);
312
+ free(del_prefix);
313
}
314
315
/*
t/t6409-merge-subtree.sh
+1
@@ -5,6 +5,7 @@ test_description='subtree merge strategy'
5
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8
+TEST_PASSES_SANITIZE_LEAK=true
9
. ./test-lib.sh
10
11
test_expect_success setup '