kmodloader: fix memory leak in print_modinfo
authorChristian Marangi <ansuelsmth@gmail.com>
Sun, 21 Jan 2024 23:49:44 +0000 (00:49 +0100)
committerChristian Marangi <ansuelsmth@gmail.com>
Sun, 21 Jan 2024 23:52:21 +0000 (00:52 +0100)
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 <ansuelsmth@gmail.com>
kmodloader.c

index 2cb6088c1fe21f0cb28e641a93244cb2b4265eb4..43105b38adea37a86df77caf55c65b385466ec13 100644 (file)
@@ -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, &params, list) {