diff mbox series

[1/2] ubox: fix GCC fanalyzer warnings

Message ID 20220718022437.1714176-1-rosenp@gmail.com
State Superseded
Headers show
Series [1/2] ubox: fix GCC fanalyzer warnings | expand

Commit Message

Rosen Penev July 18, 2022, 2:24 a.m. UTC
memory leaks and missing NULL checks.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 kmodloader.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

Comments

Jo-Philipp Wich July 18, 2022, 8:37 a.m. UTC | #1
Hi,

> [...]
> -	free(aliases);
> +	if (aliases)
> +	    free(aliases);

This check is redundant, the free() function is guaranteed to be NULL-safe in
the standard:

   The free() function shall cause the space pointed to by ptr to be
   deallocated; that is, made available for further allocation. If ptr is a
   null pointer, no action shall occur.

> [...]

~ Jo
Rosen Penev July 18, 2022, 10:14 p.m. UTC | #2
On Mon, Jul 18, 2022 at 1:45 AM Jo-Philipp Wich <jo@mein.io> wrote:
>
> Hi,
>
> > [...]
> > -     free(aliases);
> > +     if (aliases)
> > +         free(aliases);
>
> This check is redundant, the free() function is guaranteed to be NULL-safe in
> the standard:
>
>    The free() function shall cause the space pointed to by ptr to be
>    deallocated; that is, made available for further allocation. If ptr is a
>    null pointer, no action shall occur.
It seems the error was fixed in some other way. Removed.
>
> > [...]
>
> ~ Jo
>
> _______________________________________________
> openwrt-devel mailing list
> openwrt-devel@lists.openwrt.org
> https://lists.openwrt.org/mailman/listinfo/openwrt-devel
diff mbox series

Patch

diff --git a/kmodloader.c b/kmodloader.c
index 63bae5e..bc5f20c 100644
--- a/kmodloader.c
+++ b/kmodloader.c
@@ -336,6 +336,11 @@  static int scan_loaded_modules(void)
 			/* possibly a module outside /lib/modules/<ver>/ */
 			n = alloc_module(m.name, NULL, 0, m.depends, m.size);
 		}
+		if (!n) {
+			ULOG_ERR("Failed to allocate memory for module\n");
+			return -1;
+		}
+
 		n->usage = m.usage;
 		n->state = LOADED;
 	}
@@ -416,7 +421,8 @@  out:
 	if (fd >= 0)
 		close(fd);
 
-	free(aliases);
+	if (aliases)
+	    free(aliases);
 
 	return m;
 }
@@ -581,6 +587,11 @@  static int insert_module(char *path, const char *options)
 	struct stat s;
 	int fd, ret = -1;
 
+	if (!path) {
+		ULOG_ERR("Path not specified\n");
+		return ret;
+	}
+
 	if (stat(path, &s)) {
 		ULOG_ERR("missing module %s\n", path);
 		return ret;
@@ -1162,6 +1173,8 @@  load_options(void)
 			continue;
 		}
 	}
+
+	fclose(f);
 }
 
 int main(int argc, char **argv)