]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: Fix memory leak on error path
authorTobias Stoeckmann <tobias@stoeckmann.org>
Wed, 21 Aug 2024 20:06:58 +0000 (22:06 +0200)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Thu, 22 Aug 2024 21:48:33 +0000 (16:48 -0500)
If realloc fails, do not override the still valid pointer with NULL.
Otherwise freeing the iterator won't free the previously allocated
buffer.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/82
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
libkmod/libkmod-builtin.c

index 40a7d6142d03b1f303e223876cf4ea1fe5d25397..825e3a72f6953d08080ce510d89ed8793fc20fe3 100644 (file)
@@ -127,13 +127,18 @@ static off_t get_string(struct kmod_builtin_iter *iter, off_t offset,
                offset += (off_t) partsz;
 
                if (iter->bufsz < linesz + partsz) {
-                       iter->bufsz = linesz + partsz;
-                       iter->buf = realloc(iter->buf, iter->bufsz);
+                       void *tmp;
+                       size_t s;
 
-                       if (!iter->buf) {
+                       s = linesz + partsz;
+                       tmp = realloc(iter->buf, s);
+
+                       if (!tmp) {
                                sv_errno = errno;
                                goto fail;
                        }
+                       iter->bufsz = s;
+                       iter->buf = tmp;
                }
 
                strncpy(iter->buf + linesz, buf, partsz);