From 47f5be06a4793f46cc5e609261486ce072ffcb80 Mon Sep 17 00:00:00 2001 From: LiFeng Date: Sat, 12 Jun 2021 14:52:46 +0800 Subject: [PATCH] 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: LiFeng --- src/lxc/string_utils.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/lxc/string_utils.c b/src/lxc/string_utils.c index 9c54819a0..b1d6dc9bc 100644 --- a/src/lxc/string_utils.c +++ b/src/lxc/string_utils.c @@ -404,6 +404,9 @@ char **lxc_string_split_quoted(char *string) if (state == 'a') 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 *)); } @@ -443,6 +446,9 @@ char **lxc_string_split_and_trim(const char *string, char _sep) result_count++; } + if (result == NULL) + return calloc(1, sizeof(char *)); + /* if we allocated too much, reduce it */ return realloc(result, (result_count + 1) * sizeof(char *)); -- 2.47.2