]> git.ipfire.org Git - thirdparty/git.git/commitdiff
unpack-trees: clear index when not propagating it
authorPatrick Steinhardt <ps@pks.im>
Wed, 14 Aug 2024 06:52:39 +0000 (08:52 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 14 Aug 2024 17:08:00 +0000 (10:08 -0700)
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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/t3705-add-sparse-checkout.sh
unpack-trees.c

index 2bade9e804fcfc74df7d5629bbd409fda1c4a302..6ae45a788d1a97e04bf504a25efbd98a6ab5e41a 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='git add in sparse checked out working trees'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 SPARSE_ENTRY_BLOB=""
index 7dc884fafd30a36451dee70f616da4c3fe936bbb..9a55cb62040c9c243a228e6681320fbe1f4b62ee 100644 (file)
@@ -210,6 +210,7 @@ void clear_unpack_trees_porcelain(struct unpack_trees_options *opts)
 {
        strvec_clear(&opts->internal.msgs_to_free);
        memset(opts->internal.msgs, 0, sizeof(opts->internal.msgs));
+       discard_index(&opts->internal.result);
 }
 
 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
                o->internal.result.updated_workdir = 1;
                discard_index(o->dst_index);
                *o->dst_index = o->internal.result;
+               memset(&o->internal.result, 0, sizeof(o->internal.result));
        } else {
                discard_index(&o->internal.result);
        }