kmodloader: fix bad realloc usage
authorRosen Penev <rosenp@gmail.com>
Mon, 18 Jul 2022 22:12:37 +0000 (15:12 -0700)
committerHauke Mehrtens <hauke@hauke-m.de>
Sat, 13 Aug 2022 16:51:02 +0000 (18:51 +0200)
Both cppcheck and gcc's -fanalyzer complain here that realloc is being
used improperly.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
kmodloader.c

index 63bae5e827bdd25bbd60a4800451928fdb8c0c55..4b2ffa76770052bdb60a1385a33cc70b8e29472c 100644 (file)
@@ -351,6 +351,7 @@ static struct module* get_module_info(const char *module, const char *name)
        unsigned int offset, size;
        char *map = MAP_FAILED, *strings, *dep = NULL;
        const char **aliases = NULL;
+       const char **aliasesr;
        int naliases = 0;
        struct module *m = NULL;
        struct stat s;
@@ -393,12 +394,13 @@ static struct module* get_module_info(const char *module, const char *name)
                if (!strncmp(strings, "depends=", len + 1))
                        dep = sep;
                else if (!strncmp(strings, "alias=", len + 1)) {
-                       aliases = realloc(aliases, sizeof(sep) * (naliases + 1));
-                       if (!aliases) {
+                       aliasesr = realloc(aliases, sizeof(sep) * (naliases + 1));
+                       if (!aliasesr) {
                                ULOG_ERR("out of memory\n");
                                goto out;
                        }
 
+                       aliases = aliasesr;
                        aliases[naliases++] = sep;
                }
                strings = &sep[strlen(sep)];