common: Add NULL checks for xrealloc in make_string cli_hush.c
authorAnton Moryakov <ant.v.moryakov@gmail.com>
Thu, 6 Feb 2025 22:01:23 +0000 (01:01 +0300)
committerTom Rini <trini@konsulko.com>
Tue, 11 Feb 2025 14:17:23 +0000 (08:17 -0600)
- Check return value of xrealloc for NULL.
- Free allocated memory and return NULL if xrealloc fails.
- Prevent NULL pointer dereference in strlen and strcat.

Triggers found by static analyzer Svace.

Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
common/cli_hush.c

index a6a8edc..9f437ae 100644 (file)
@@ -3626,7 +3626,13 @@ static char *make_string(char **inp, int *nonnull)
                noeval = 1;
        for (n = 0; inp[n]; n++) {
                p = insert_var_value_sub(inp[n], noeval);
-               str = xrealloc(str, (len + strlen(p) + (2 * nonnull[n])));
+               char *new_str = xrealloc(str, (len + strlen(p) + (2 * nonnull[n])));
+               if (!new_str) {
+                       free(str);
+                       if (p != inp[n]) free(p);
+                       return NULL;
+               }
+               str = new_str;
                if (n) {
                        strcat(str, " ");
                } else {