Skip to content

Commit

Permalink
reftable/basics: fix segfault when growing names array fails
Browse files Browse the repository at this point in the history
When growing the `names` array fails we would end up with a `NULL`
pointer. This causes two problems:

  - We would run into a segfault because we try to free names that we
    have assigned to the array already.

  - We lose track of the old array and cannot free its contents.

Fix this issue by using a temporary variable. Like this we do not
clobber the old array that we tried to reallocate, which will remain
valid when a call to realloc(3P) fails.

Signed-off-by: Patrick Steinhardt <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
pks-t authored and gitster committed Oct 4, 2024
1 parent 3573030 commit 2179b5c
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions reftable/basics.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,11 @@ char **parse_names(char *buf, int size)
next = end;
}
if (p < next) {
REFTABLE_ALLOC_GROW(names, names_len + 1, names_cap);
if (!names)
char **names_grown = names;
REFTABLE_ALLOC_GROW(names_grown, names_len + 1, names_cap);
if (!names_grown)
goto err;
names = names_grown;

names[names_len] = reftable_strdup(p);
if (!names[names_len++])
Expand Down

0 comments on commit 2179b5c

Please sign in to comment.