[PATCH] perf trace: free intlist on filter_pids calloc failure

From: Gu Bin

Date: Thu Nov 12 2026 - 03:35:10 EST


trace__set_filter_pids_from_option() builds an intlist and stores its
entry count in trace->filter_pids.nr before allocating the entries
array. If that calloc fails, the function jumps to the out label,
which returns without freeing the intlist: the option callback exits
perf with an error, so the leak is the only damage, but it is
triggered on every --filter-pids run under memory pressure.

Restructure the function so that every path that has created the
intlist also frees it: the failure path now falls through to
intlist__delete() as well, and the out label is gone.

Fixes: f078c3852c73 ("perf trace: Introduce --filter-pids")
Signed-off-by: Gu Bin <gubin@xxxxxxxxxxxxxxxxxxxx>
---
tools/perf/builtin-trace.c | 13 ++++++-------
1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
index 4094141461f91..d435b35f1351c 100644
--- a/tools/perf/builtin-trace.c
+++ b/tools/perf/builtin-trace.c
@@ -5432,17 +5432,16 @@ static int trace__set_filter_pids_from_option(const struct option *opt, const ch
i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
trace->filter_pids.entries = calloc(i, sizeof(pid_t));

- if (trace->filter_pids.entries == NULL)
- goto out;
+ if (trace->filter_pids.entries) {
+ trace->filter_pids.entries[0] = getpid();

- trace->filter_pids.entries[0] = getpid();
+ for (i = 1; i < trace->filter_pids.nr; ++i)
+ trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;

- for (i = 1; i < trace->filter_pids.nr; ++i)
- trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
+ ret = 0;
+ }

intlist__delete(list);
- ret = 0;
-out:
return ret;
}

--
2.50.1.windows.1