]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
tools: Handle realloc failure in strlist_add
authorFrancois Berder <fberder@outlook.fr>
Mon, 19 Jan 2026 14:49:06 +0000 (15:49 +0100)
committerTom Rini <trini@konsulko.com>
Wed, 28 Jan 2026 20:41:21 +0000 (14:41 -0600)
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>
tools/image-host.c

index 54df86316ae12366890b479936d772bcc1bcd7ed..48d69191c92150fe45a893e11ddffaf03e6bf031 100644 (file)
@@ -733,6 +733,7 @@ static void strlist_free(struct strlist *list)
 static int strlist_add(struct strlist *list, const char *str)
 {
        char *dup;
+       char  **tmp = NULL;
 
        if (!list || !str)
                return -1;
@@ -741,13 +742,13 @@ static int strlist_add(struct strlist *list, const char *str)
        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;