Commit 47f5be06 by LiFeng

string utils: Make sure don't return uninitialized memory.

The function lxc_string_split_quoted and lxc_string_split_and_trim use realloc to reduce the memory. But the result may be NULL, the the returned memory will be uninitialized Signed-off-by: 's avatarLiFeng <lifeng68@huawei.com>
parent a197d2fb
...@@ -404,6 +404,9 @@ char **lxc_string_split_quoted(char *string) ...@@ -404,6 +404,9 @@ char **lxc_string_split_quoted(char *string)
if (state == 'a') if (state == 'a')
complete_word(&result, nextword, p, &result_capacity, &result_count); complete_word(&result, nextword, p, &result_capacity, &result_count);
if (result == NULL)
return calloc(1, sizeof(char *));
return realloc(result, (result_count + 1) * sizeof(char *)); return realloc(result, (result_count + 1) * sizeof(char *));
} }
...@@ -443,6 +446,9 @@ char **lxc_string_split_and_trim(const char *string, char _sep) ...@@ -443,6 +446,9 @@ char **lxc_string_split_and_trim(const char *string, char _sep)
result_count++; result_count++;
} }
if (result == NULL)
return calloc(1, sizeof(char *));
/* if we allocated too much, reduce it */ /* if we allocated too much, reduce it */
return realloc(result, (result_count + 1) * sizeof(char *)); return realloc(result, (result_count + 1) * sizeof(char *));
......
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