Commit c5c63275 by Christian Brauner Committed by Stéphane Graber

utils: fix lxc_string_split()

Make sure we don't return uninitialized memory. Signed-off-by: 's avatarChristian Brauner <christian.brauner@canonical.com>
parent 75275a1e
...@@ -705,8 +705,8 @@ bool lxc_string_in_list(const char *needle, const char *haystack, char _sep) ...@@ -705,8 +705,8 @@ bool lxc_string_in_list(const char *needle, const char *haystack, char _sep)
char **lxc_string_split(const char *string, char _sep) char **lxc_string_split(const char *string, char _sep)
{ {
char *token, *str, *saveptr = NULL; char *token, *str, *saveptr = NULL;
char sep[2] = { _sep, '\0' }; char sep[2] = {_sep, '\0'};
char **result = NULL; char **tmp = NULL, **result = NULL;
size_t result_capacity = 0; size_t result_capacity = 0;
size_t result_count = 0; size_t result_count = 0;
int r, saved_errno; int r, saved_errno;
...@@ -714,7 +714,7 @@ char **lxc_string_split(const char *string, char _sep) ...@@ -714,7 +714,7 @@ char **lxc_string_split(const char *string, char _sep)
if (!string) if (!string)
return calloc(1, sizeof(char *)); return calloc(1, sizeof(char *));
str = alloca(strlen(string)+1); str = alloca(strlen(string) + 1);
strcpy(str, string); strcpy(str, string);
for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) { for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) {
r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16); r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16);
...@@ -727,7 +727,14 @@ char **lxc_string_split(const char *string, char _sep) ...@@ -727,7 +727,14 @@ char **lxc_string_split(const char *string, char _sep)
} }
/* if we allocated too much, reduce it */ /* 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: error_out:
saved_errno = errno; saved_errno = errno;
lxc_free_array((void **)result, free); lxc_free_array((void **)result, free);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment