Skip to content

Commit 2179b5c

Browse files
pks-tgitster
authored andcommitted
reftable/basics: fix segfault when growing names array fails
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]>
1 parent 3573030 commit 2179b5c

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

reftable/basics.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,11 @@ char **parse_names(char *buf, int size)
152152
next = end;
153153
}
154154
if (p < next) {
155-
REFTABLE_ALLOC_GROW(names, names_len + 1, names_cap);
156-
if (!names)
155+
char **names_grown = names;
156+
REFTABLE_ALLOC_GROW(names_grown, names_len + 1, names_cap);
157+
if (!names_grown)
157158
goto err;
159+
names = names_grown;
158160

159161
names[names_len] = reftable_strdup(p);
160162
if (!names[names_len++])

0 commit comments

Comments
 (0)