aboutsummaryrefslogtreecommitdiffstats
path: root/reftable/stack.c
diff options
context:
space:
mode:
authorRené Scharfe <l.s.r@web.de>2024-12-28 10:47:05 +0100
committerJunio C Hamano <gitster@pobox.com>2024-12-28 08:00:44 -0800
commit8db127d43f5b0eff254a851f9c966b7b85d91992 (patch)
tree8c7cd2c2a66573512ade8f4e80f5af8e88fe1e4b /reftable/stack.c
parentMerge https://github.com/j6t/git-gui (diff)
downloadgit-8db127d43f5b0eff254a851f9c966b7b85d91992.tar.gz
git-8db127d43f5b0eff254a851f9c966b7b85d91992.zip
reftable: avoid leaks on realloc error
When realloc(3) fails, it returns NULL and keeps the original allocation intact. REFTABLE_ALLOC_GROW overwrites both the original pointer and the allocation count variable in that case, simultaneously leaking the original allocation and misrepresenting the number of storable items. parse_names() and reftable_buf_add() avoid leaking by restoring the original pointer value on failure, but all other callers seem to be OK with losing the old allocation. Add a new variant of the macro, REFTABLE_ALLOC_GROW_OR_NULL, which plugs the leak and zeros the allocation counter. Use it for those callers. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'reftable/stack.c')
-rw-r--r--reftable/stack.c8
1 files changed, 5 insertions, 3 deletions
diff --git a/reftable/stack.c b/reftable/stack.c
index 634f0c5425..531660a49f 100644
--- a/reftable/stack.c
+++ b/reftable/stack.c
@@ -317,7 +317,9 @@ static int reftable_stack_reload_once(struct reftable_stack *st,
* thus need to keep them alive here, which we
* do by bumping their refcount.
*/
- REFTABLE_ALLOC_GROW(reused, reused_len + 1, reused_alloc);
+ REFTABLE_ALLOC_GROW_OR_NULL(reused,
+ reused_len + 1,
+ reused_alloc);
if (!reused) {
err = REFTABLE_OUT_OF_MEMORY_ERROR;
goto done;
@@ -949,8 +951,8 @@ int reftable_addition_add(struct reftable_addition *add,
if (err < 0)
goto done;
- REFTABLE_ALLOC_GROW(add->new_tables, add->new_tables_len + 1,
- add->new_tables_cap);
+ REFTABLE_ALLOC_GROW_OR_NULL(add->new_tables, add->new_tables_len + 1,
+ add->new_tables_cap);
if (!add->new_tables) {
err = REFTABLE_OUT_OF_MEMORY_ERROR;
goto done;