[OpenWrt-Devel] [PATCH uci 10/18] cli: fix realloc issue spotted by cppcheck

Petr Štetiar ynezz at true.cz
Mon Nov 4 19:36:49 EST 2019


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 at true.cz>
---
 cli.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/cli.c b/cli.c
index f8b45dba091f..1ce4d5ed1d33 100644
--- 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)

_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel


More information about the openwrt-devel mailing list