]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
depmod: Release memory on error path
authorTobias Stoeckmann <tobias@stoeckmann.org>
Wed, 6 Nov 2024 16:50:27 +0000 (17:50 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Fri, 15 Nov 2024 18:59:45 +0000 (12:59 -0600)
Check malloc return value and clean up resources if allocation fails.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/228
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
tools/depmod.c

index dc4b8a2f149b01706e38e70f99ab46ac44289ec4..a3b75ad8d329d70176fc0baac355234653318283 100644 (file)
@@ -1802,7 +1802,7 @@ static int depmod_report_one_cycle(struct depmod *depmod, struct vertex *vertex,
        int i;
        int n;
        struct vertex *v;
-       int rc;
+       int rc = 0;
 
        array_init(&reverse, 3);
 
@@ -1820,6 +1820,10 @@ static int depmod_report_one_cycle(struct depmod *depmod, struct vertex *vertex,
        sz += vertex->mod->modnamesz - 1;
 
        buf = malloc(sz + n * strlen(sep) + 1);
+       if (buf == NULL) {
+               rc = -ENOMEM;
+               goto out;
+       }
 
        sz = 0;
        for (i = reverse.count - 1; i >= 0; i--) {
@@ -1839,9 +1843,10 @@ static int depmod_report_one_cycle(struct depmod *depmod, struct vertex *vertex,
        ERR("Cycle detected: %s\n", buf);
 
        free(buf);
+out:
        array_free_array(&reverse);
 
-       return 0;
+       return rc;
 }
 
 static int depmod_report_cycles_from_root(struct depmod *depmod, struct mod *root_mod,