Skip to content

Commit

Permalink
utils: fix lxc_string_split()
Browse files Browse the repository at this point in the history
Make sure we don't return uninitialized memory.

Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
  • Loading branch information
Christian Brauner authored and stgraber committed Oct 3, 2016
1 parent 07d1127 commit 304b20d
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/lxc/utils.c
Expand Up @@ -774,16 +774,16 @@ bool lxc_string_in_list(const char *needle, const char *haystack, char _sep)
char **lxc_string_split(const char *string, char _sep)
{
char *token, *str, *saveptr = NULL;
char sep[2] = { _sep, '\0' };
char **result = NULL;
char sep[2] = {_sep, '\0'};
char **tmp = NULL, **result = NULL;
size_t result_capacity = 0;
size_t result_count = 0;
int r, saved_errno;

if (!string)
return calloc(1, sizeof(char *));

str = alloca(strlen(string)+1);
str = alloca(strlen(string) + 1);
strcpy(str, string);
for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) {
r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16);
Expand All @@ -796,7 +796,14 @@ char **lxc_string_split(const char *string, char _sep)
}

/* if we allocated too much, reduce it */
return realloc(result, (result_count + 1) * sizeof(char *));
tmp = realloc(result, (result_count + 1) * sizeof(char *));
if (!tmp)
goto error_out;
result = tmp;
/* Make sure we don't return uninitialized memory. */
if (result_count == 0)
*result = NULL;
return result;
error_out:
saved_errno = errno;
lxc_free_array((void **)result, free);
Expand Down

0 comments on commit 304b20d

Please sign in to comment.