If realloc fails, list->strings was set to NULL and
it would create a leak. This commit ensures that if we cannot
add a string to the list, the list stays in a good state.
Signed-off-by: Francois Berder <fberder@outlook.fr>
static int strlist_add(struct strlist *list, const char *str)
{
char *dup;
+ char **tmp = NULL;
if (!list || !str)
return -1;
if(!dup)
return -1;
- list->strings = realloc(list->strings,
- (list->count + 1) * sizeof(char *));
- if (!list->strings) {
+ tmp = realloc(list->strings, (list->count + 1) * sizeof(char *));
+ if (!tmp) {
free(dup);
return -1;
}
+ list->strings = tmp;
list->strings[list->count++] = dup;
return 0;