* [PATCH v2] scripts/tracepoint-update: fix memory leak in add_string() on failure
@ 2026-01-19 11:45 Weigang He
2026-01-20 10:00 ` Markus Elfring
2026-01-21 2:22 ` Masami Hiramatsu
0 siblings, 2 replies; 4+ messages in thread
From: Weigang He @ 2026-01-19 11:45 UTC (permalink / raw)
To: Steven Rostedt, Masami Hiramatsu
Cc: Mathieu Desnoyers, linux-kernel, linux-trace-kernel, Weigang He
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.
Signed-off-by: Weigang He <geoffreyhe2@gmail.com>
---
scripts/tracepoint-update.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/scripts/tracepoint-update.c b/scripts/tracepoint-update.c
index 90046aedc97b9..5cf43c0aac891 100644
--- a/scripts/tracepoint-update.c
+++ b/scripts/tracepoint-update.c
@@ -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;
--
2.34.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2] scripts/tracepoint-update: fix memory leak in add_string() on failure
2026-01-19 11:45 [PATCH v2] scripts/tracepoint-update: fix memory leak in add_string() on failure Weigang He
@ 2026-01-20 10:00 ` Markus Elfring
2026-01-21 2:22 ` Masami Hiramatsu
1 sibling, 0 replies; 4+ messages in thread
From: Markus Elfring @ 2026-01-20 10:00 UTC (permalink / raw)
To: Weigang He, linux-trace-kernel
Cc: LKML, Masami Hiramatsu, Mathieu Desnoyers, Steven Rostedt
…
> This bug is found by my static analysis tool …
Will any additional background information become more helpful here?
> ---
> scripts/tracepoint-update.c | 2 ++
…
Some contributors would appreciate patch version descriptions.
https://lore.kernel.org/all/?q=%22This+looks+like+a+new+version+of+a+previously+submitted+patch%22
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.19-rc6#n310
Regards,
Markus
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] scripts/tracepoint-update: fix memory leak in add_string() on failure
2026-01-19 11:45 [PATCH v2] scripts/tracepoint-update: fix memory leak in add_string() on failure Weigang He
2026-01-20 10:00 ` Markus Elfring
@ 2026-01-21 2:22 ` Masami Hiramatsu
2026-01-23 17:42 ` Steven Rostedt
1 sibling, 1 reply; 4+ messages in thread
From: Masami Hiramatsu @ 2026-01-21 2:22 UTC (permalink / raw)
To: Weigang He
Cc: Steven Rostedt, Mathieu Desnoyers, linux-kernel,
linux-trace-kernel
On Mon, 19 Jan 2026 11:45:42 +0000
Weigang He <geoffreyhe2@gmail.com> wrote:
> 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 looks not enough. If the memory allocation is failed, it should NOT
continue anything.
I think we need to make the command itself failure when it fails to
allocate memory, as below:
diff --git a/scripts/tracepoint-update.c b/scripts/tracepoint-update.c
index 90046aedc97b..1b4129a21942 100644
--- a/scripts/tracepoint-update.c
+++ b/scripts/tracepoint-update.c
@@ -94,7 +94,7 @@ static void make_trace_array(struct elf_tracepoint *etrace)
if (!len)
continue;
if (add_string(str, &vals, &count) < 0)
- return;
+ exit(EXIT_FAILURE);
}
/* If CONFIG_TRACEPOINT_VERIFY_USED is not set, there's nothing to do */
Thank you,
>
> This bug is found by my static analysis tool and my code review.
>
> Signed-off-by: Weigang He <geoffreyhe2@gmail.com>
> ---
> scripts/tracepoint-update.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/scripts/tracepoint-update.c b/scripts/tracepoint-update.c
> index 90046aedc97b9..5cf43c0aac891 100644
> --- a/scripts/tracepoint-update.c
> +++ b/scripts/tracepoint-update.c
> @@ -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;
> --
> 2.34.1
>
--
Masami Hiramatsu (Google) <mhiramat@kernel.org>
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2] scripts/tracepoint-update: fix memory leak in add_string() on failure
2026-01-21 2:22 ` Masami Hiramatsu
@ 2026-01-23 17:42 ` Steven Rostedt
0 siblings, 0 replies; 4+ messages in thread
From: Steven Rostedt @ 2026-01-23 17:42 UTC (permalink / raw)
To: Masami Hiramatsu (Google)
Cc: Weigang He, Mathieu Desnoyers, linux-kernel, linux-trace-kernel
On Wed, 21 Jan 2026 11:22:53 +0900
Masami Hiramatsu (Google) <mhiramat@kernel.org> wrote:
> >
> > 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 looks not enough. If the memory allocation is failed, it should NOT
> continue anything.
>
> I think we need to make the command itself failure when it fails to
> allocate memory, as below:
That's a separate bug. I'm taking this current patch as-is as a fix for the
leak. Not stopping immediately is a separate issue.
-- Steve
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-01-23 17:42 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-19 11:45 [PATCH v2] scripts/tracepoint-update: fix memory leak in add_string() on failure Weigang He
2026-01-20 10:00 ` Markus Elfring
2026-01-21 2:22 ` Masami Hiramatsu
2026-01-23 17:42 ` Steven Rostedt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox