kmodloader: fix memory leak in scan_loaded_modules()
authorTony Ambardar <itugrok@yahoo.com>
Mon, 15 Jan 2024 20:29:48 +0000 (12:29 -0800)
committerTony Ambardar <itugrok@yahoo.com>
Tue, 16 Jan 2024 01:53:18 +0000 (17:53 -0800)
Commit 4c7b720b9c63 fixed some error-handling paths but introduced another
memory leak; update and fix.

Fixes: 4c7b720b9c63 ("kmodloader: fix GCC fanalyzer warnings")
Signed-off-by: Tony Ambardar <itugrok@yahoo.com>
kmodloader.c

index 259ac52d8fa9ce1a4aab43e704525d8583a756c0..0afc79560693e9a67bf0ca74a0dfd934b5387e75 100644 (file)
@@ -311,12 +311,13 @@ static int scan_loaded_modules(void)
 {
        size_t buf_len = 0;
        char *buf = NULL;
+       int rv = -1;
        FILE *fp;
 
        fp = fopen("/proc/modules", "r");
        if (!fp) {
                ULOG_ERR("failed to open /proc/modules\n");
-               return -1;
+               goto out;
        }
 
        while (getline(&buf, &buf_len, fp) > 0) {
@@ -338,16 +339,18 @@ static int scan_loaded_modules(void)
                }
                if (!n) {
                        ULOG_ERR("Failed to allocate memory for module\n");
-                       return -1;
+                       goto out;
                }
 
                n->usage = m.usage;
                n->state = LOADED;
        }
+       rv = 0;
+out:
        free(buf);
        fclose(fp);
 
-       return 0;
+       return rv;
 }
 
 static struct module* get_module_info(const char *module, const char *name)