reftable/basics: handle allocation failures in parse_names()

Handle allocation failures in `parse_names()` by returning `NULL` in
case any allocation fails. While at it, refactor the function to return
the array directly instead of assigning it to an out-pointer.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt
2024-10-02 12:55:38 +02:00
committed by Junio C Hamano
parent 6593e147d3
commit eef7bcdafe
4 changed files with 33 additions and 13 deletions

View File

@ -135,14 +135,14 @@ size_t names_length(const char **names)
return p - names;
}
void parse_names(char *buf, int size, char ***namesp)
char **parse_names(char *buf, int size)
{
char **names = NULL;
size_t names_cap = 0;
size_t names_len = 0;
char *p = buf;
char *end = buf + size;
while (p < end) {
char *next = strchr(p, '\n');
if (next && next < end) {
@ -152,14 +152,26 @@ void parse_names(char *buf, int size, char ***namesp)
}
if (p < next) {
REFTABLE_ALLOC_GROW(names, names_len + 1, names_cap);
names[names_len++] = xstrdup(p);
if (!names)
goto err;
names[names_len] = reftable_strdup(p);
if (!names[names_len++])
goto err;
}
p = next + 1;
}
REFTABLE_REALLOC_ARRAY(names, names_len + 1);
names[names_len] = NULL;
*namesp = names;
return names;
err:
for (size_t i = 0; i < names_len; i++)
reftable_free(names[i]);
reftable_free(names);
return NULL;
}
int names_equal(const char **a, const char **b)