Skip to content

Commit 4dfd4f1

Browse files
pks-tgitster
authored andcommitted
unpack-trees: clear index when not propagating it
When provided a pointer to a destination index, then `unpack_trees()` will end up copying its `o->internal.result` index into the provided pointer. In those cases it is thus not necessary to free the index, as we have transferred ownership of it. There are cases though where we do not end up transferring ownership of the memory, but `clear_unpack_trees_porcelain()` will never discard the index in that case and thus cause a memory leak. And right now it cannot do so in the first place because we have no indicator of whether we did or didn't transfer ownership of the index. Adapt the code to zero out the index in case we transfer its ownership. Like this, we can now unconditionally discard the index when being asked to clear the `unpack_trees_options`. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2f07d22 commit 4dfd4f1

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

t/t3705-add-sparse-checkout.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
test_description='git add in sparse checked out working trees'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67

78
SPARSE_ENTRY_BLOB=""

unpack-trees.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ void clear_unpack_trees_porcelain(struct unpack_trees_options *opts)
210210
{
211211
strvec_clear(&opts->internal.msgs_to_free);
212212
memset(opts->internal.msgs, 0, sizeof(opts->internal.msgs));
213+
discard_index(&opts->internal.result);
213214
}
214215

215216
static int do_add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
@@ -2082,6 +2083,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
20822083
o->internal.result.updated_workdir = 1;
20832084
discard_index(o->dst_index);
20842085
*o->dst_index = o->internal.result;
2086+
memset(&o->internal.result, 0, sizeof(o->internal.result));
20852087
} else {
20862088
discard_index(&o->internal.result);
20872089
}

0 commit comments

Comments
 (0)