kmodloader: fix compilation warning with not checking return of asprintf
authorChristian Marangi <ansuelsmth@gmail.com>
Sun, 17 Jul 2022 00:59:32 +0000 (02:59 +0200)
committerChristian Marangi <ansuelsmth@gmail.com>
Sun, 17 Jul 2022 00:59:32 +0000 (02:59 +0200)
Fix the following compilation warning:

kmodloader.c: In function 'main_loader':
kmodloader.c:1027:41: error: ignoring return value of 'asprintf' declared with attribute 'warn_unused_result' [-Werror=unused-result]
make[1]: *** [package/Makefile:116: package/system/ubox/compile] Error 1
 1027 |                                         asprintf(&m->opts, "%s %s", prev, opts);
      |                                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

While at it rework the function to not duplicate too much code with the
error handling.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
kmodloader.c

index 26926a021be474bac5fd7e9db07aa8b79e43602a..63bae5e827bdd25bbd60a4800451928fdb8c0c55 100644 (file)
@@ -965,7 +965,7 @@ static int main_loader(int argc, char **argv)
        struct module *m;
        glob_t gl;
        char *path;
-       int fail, j;
+       int ret = 0, fail, j;
 
        if (argc > 1)
                dir = argv[1];
@@ -980,13 +980,13 @@ static int main_loader(int argc, char **argv)
        strcat(path, "*");
 
        if (scan_module_folders()) {
-               free (path);
-               return -1;
+               ret = -1;
+               goto free_path;
        }
 
        if (scan_loaded_modules()) {
-               free (path);
-               return -1;
+               ret = -1;
+               goto free_path;
        }
 
        ULOG_INFO("loading kernel modules from %s\n", path);
@@ -1024,8 +1024,15 @@ static int main_loader(int argc, char **argv)
                                if (m->opts) {
                                        char *prev = m->opts;
 
-                                       asprintf(&m->opts, "%s %s", prev, opts);
+                                       fail = asprintf(&m->opts, "%s %s", prev, opts);
                                        free(prev);
+                                       if (fail < 0) {
+                                               ULOG_ERR("out of memory for opts %s\n", opts);
+                                               free(mod);
+                                               fclose(fp);
+                                               ret = -1;
+                                               goto out;
+                                       }
                                } else {
                                        m->opts = strdup(opts);
                                }
@@ -1058,9 +1065,10 @@ static int main_loader(int argc, char **argv)
 
 out:
        globfree(&gl);
+free_path:
        free(path);
 
-       return 0;
+       return ret;
 }
 
 static inline char weight(char c)