Skip to content

Commit 2a01470

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 68bd0a9 commit 2a01470

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

match-trees.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,18 +294,22 @@ void shift_tree(struct repository *r,
294294
unsigned short mode;
295295

296296
if (!*del_prefix)
297-
return;
297+
goto out;
298298

299299
if (get_tree_entry(r, hash2, del_prefix, shifted, &mode))
300300
die("cannot find path %s in tree %s",
301301
del_prefix, oid_to_hex(hash2));
302-
return;
302+
goto out;
303303
}
304304

305305
if (!*add_prefix)
306-
return;
306+
goto out;
307307

308308
splice_tree(hash1, add_prefix, hash2, shifted);
309+
310+
out:
311+
free(add_prefix);
312+
free(del_prefix);
309313
}
310314

311315
/*

t/t6409-merge-subtree.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test_description='subtree merge strategy'
55
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
66
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
77

8+
TEST_PASSES_SANITIZE_LEAK=true
89
. ./test-lib.sh
910

1011
test_expect_success setup '

0 commit comments

Comments
 (0)