From: Christian Marangi Date: Sun, 21 Jan 2024 23:49:44 +0000 (+0100) Subject: kmodloader: fix memory leak in print_modinfo X-Git-Url: http://git.openwrt.org/openwrt/docs.git?a=commitdiff_plain;h=202d7c05029a43f5665cd81355d8abdfdec2bb2d;p=project%2Fubox.git kmodloader: fix memory leak in print_modinfo Fix memory leak in print_modinfo reported by Coverity Report. It seems there is a logic error and duplicated string in print_modinfo is never freed. On top of this if the while loop terminates early the just allocated duplicated string is also never freed. Rework the function to correctly free the duplicated string. Fix CID 1586644: Resource leaks (RESOURCE_LEAK). Signed-off-by: Christian Marangi --- diff --git a/kmodloader.c b/kmodloader.c index 2cb6088..43105b3 100644 --- a/kmodloader.c +++ b/kmodloader.c @@ -649,8 +649,11 @@ static int print_modinfo(const struct module *m) printf("%s:\t%s\n", dup, sep); } else { sep2 = strstr(sep, ":"); - if (!sep2) + if (!sep2) { + free(dup); break; + } + pname = strndup(sep, sep2 - sep); sep2++; pdata = strdup(sep2); @@ -673,10 +676,10 @@ static int print_modinfo(const struct module *m) else p->desc = pdata; } + + free(dup); next_string: strings = &sep[strlen(sep)]; - if (dup) - free(dup); } list_for_each_entry(p, ¶ms, list) {