From 202d7c05029a43f5665cd81355d8abdfdec2bb2d Mon Sep 17 00:00:00 2001 From: Christian Marangi Date: Mon, 22 Jan 2024 00:49:44 +0100 Subject: [PATCH] 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 --- kmodloader.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) 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) { -- 2.30.2