]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
scripts/tracepoint-update: Fix memory leak in add_string() on failure
authorWeigang He <geoffreyhe2@gmail.com>
Mon, 19 Jan 2026 11:45:42 +0000 (11:45 +0000)
committerSteven Rostedt (Google) <rostedt@goodmis.org>
Fri, 23 Jan 2026 18:34:45 +0000 (13:34 -0500)
When realloc() fails in add_string(), the function returns -1 but leaves
*vals pointing to the previously allocated memory. This can cause memory
leaks in callers like make_trace_array() that return on error without
freeing the partially built array.

Fix this by freeing *vals and setting it to NULL when realloc() fails.
This makes the error handling self-contained in add_string() so callers
don't need to handle cleanup on failure.

This bug is found by my static analysis tool and my code review.

Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fixes: e30f8e61e2518 ("tracing: Add a tracepoint verification check at build time")
Link: https://patch.msgid.link/20260119114542.1714405-1-geoffreyhe2@gmail.com
Signed-off-by: Weigang He <geoffreyhe2@gmail.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
scripts/tracepoint-update.c

index 90046aedc97b97b0023fccba94971cf054f8087a..5cf43c0aac891c9d3a42a7865a5729760486d5ee 100644 (file)
@@ -49,6 +49,8 @@ static int add_string(const char *str, const char ***vals, int *count)
                array = realloc(array, sizeof(char *) * size);
                if (!array) {
                        fprintf(stderr, "Failed memory allocation\n");
+                       free(*vals);
+                       *vals = NULL;
                        return -1;
                }
                *vals = array;