split-index: fix memory leak in `move_cache_to_base_index()`

In `move_cache_to_base_index()` we move the index cache of the main index into the split index, which is used when writing a shared index. But we don't release the old split index base in case we already had a split index before this operation, which can thus leak memory. Plug the leak by releasing the previous base. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Nov 20, 2024 at 14:39 UTC a5408d1820e0ea75612bbd6f6a6c495066e5ffcb
2 files changed +6 -1
split-index.c
+5 -1
@@ -97,7 +97,11 @@ void move_cache_to_base_index(struct index_state *istate)
97 mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool);
98 }
99
100 - ALLOC_ARRAY(si->base, 1);
100 + if (si->base)
101 + release_index(si->base);
102 + else
103 + ALLOC_ARRAY(si->base, 1);
104 +
105 index_state_init(si->base, istate->repo);
106 si->base->version = istate->version;
107 /* zero timestamp disables racy test in ce_write_index() */
t/t1700-split-index.sh
+1
@@ -5,6 +5,7 @@ test_description='split index mode tests'
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 # We need total control of index splitting here