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>
This commit is contained in:
René Scharfe
2024-12-28 10:47:05 +01:00
committed by Junio C Hamano
parent 76cf4f61c8
commit 8db127d43f
7 changed files with 61 additions and 16 deletions

View File

@ -129,6 +129,16 @@ char *reftable_strdup(const char *str);
REFTABLE_REALLOC_ARRAY(x, alloc); \
} \
} while (0)
#define REFTABLE_ALLOC_GROW_OR_NULL(x, nr, alloc) do { \
void *reftable_alloc_grow_or_null_orig_ptr = (x); \
REFTABLE_ALLOC_GROW((x), (nr), (alloc)); \
if (!(x)) { \
reftable_free(reftable_alloc_grow_or_null_orig_ptr); \
alloc = 0; \
} \
} while (0)
#define REFTABLE_FREE_AND_NULL(p) do { reftable_free(p); (p) = NULL; } while (0)
#ifndef REFTABLE_ALLOW_BANNED_ALLOCATORS