]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: release memory on builtin error path
authorTobias Stoeckmann <tobias@stoeckmann.org>
Sat, 22 Feb 2025 18:53:43 +0000 (19:53 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Tue, 11 Mar 2025 18:46:33 +0000 (13:46 -0500)
If the modules.builtin.modinfo file contains valid and invalid
lines, it is possible that libkmod leaks memory on error path.

Let strbuf API take care to always release strbuf and reset it
if allocated heap memory is taken away from it.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/291
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
(cherry picked from commit 51929659b2faad1288bea9502cb5a672ca15abff)

libkmod/libkmod-builtin.c

index 293ea894456b7215ccb64514b42fc399c29bdc12..f354d455d8ae631a0b6835552c1fecc1543db265 100644 (file)
@@ -150,9 +150,10 @@ static char **strbuf_to_vector(struct strbuf *buf, size_t count)
        vector = realloc(buf->bytes, total_size);
        if (vector == NULL)
                return NULL;
-       buf->bytes = NULL;
+
        memmove(vector + count + 1, vector, strbuf_used(buf));
        s = (char *)(vector + count + 1);
+       strbuf_init(buf);
 
        for (n = 0; n < count; n++) {
                vector[n] = s;
@@ -167,13 +168,12 @@ static char **strbuf_to_vector(struct strbuf *buf, size_t count)
 ssize_t kmod_builtin_get_modinfo(struct kmod_ctx *ctx, const char *modname,
                                 char ***modinfo)
 {
+       DECLARE_STRBUF(buf);
        struct kmod_builtin_info info;
-       struct strbuf buf;
        ssize_t count;
 
        if (!kmod_builtin_info_init(&info, ctx))
                return -errno;
-       strbuf_init(&buf);
 
        count = get_strings(&info, modname, &buf);
        if (count == 0)
@@ -183,7 +183,6 @@ ssize_t kmod_builtin_get_modinfo(struct kmod_ctx *ctx, const char *modname,
                if (*modinfo == NULL) {
                        count = -errno;
                        ERR(ctx, "strbuf_to_vector: %s\n", strerror(errno));
-                       strbuf_release(&buf);
                }
        }