cli: fix realloc issue spotted by cppcheck
authorPetr Štetiar <ynezz@true.cz>
Mon, 4 Nov 2019 18:46:13 +0000 (19:46 +0100)
committerPetr Štetiar <ynezz@true.cz>
Thu, 14 Nov 2019 16:11:34 +0000 (17:11 +0100)
Cppcheck 1.90 dev reports following:

 cli.c:117:4: error: Common realloc mistake: 'typestr' nulled but not freed upon failure [memleakOnRealloc]
    typestr = realloc(typestr, maxlen);
    ^

Signed-off-by: Petr Štetiar <ynezz@true.cz>
cli.c

diff --git a/cli.c b/cli.c
index f8b45dba091f088f84e22dc98503fe9e6e1fd7e3..1ce4d5ed1d334b913b3ceaa2e1925e33264fe1d8 100644 (file)
--- a/cli.c
+++ b/cli.c
@@ -113,8 +113,16 @@ uci_lookup_section_ref(struct uci_section *s)
                maxlen = strlen(s->type) + 1 + 2 + 10;
                if (!typestr) {
                        typestr = malloc(maxlen);
+                       if (!typestr)
+                               return NULL;
                } else {
-                       typestr = realloc(typestr, maxlen);
+                       void *p = realloc(typestr, maxlen);
+                       if (!p) {
+                               free(typestr);
+                               return NULL;
+                       }
+
+                       typestr = p;
                }
 
                if (typestr)