[PATCH v2 01/12] perf header: Fix memory leaks in process_cpu_domain_info

Ian Rogers
2026-01-22T16:54:21Z

do_read_string returns a string in allocated memory, for some reason
there was unused memory allocations and unnecessary strdups. Remove
these and make the "perf annotate basic tests" leak sanitizer clean.

Signed-off-by: Ian Rogers <irogers@google.com>
Fixes: d40c68a49f69 ("perf header: Support CPU DOMAIN relation info")
---
tools/perf/util/header.c | 17 ++++-------------
1 file changed, 4 insertions(+), 13 deletions(-)

diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 9a15dd4b7640..eefd1cd73b6a 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -3634,6 +3634,7 @@ static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unused
if (!d_info)
return -1;

+ assert(cd_map[cpu]->domains[domain] == NULL);
cd_map[cpu]->domains[domain] = d_info;
d_info->domain = domain;

@@ -3642,30 +3643,20 @@ static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unused
if (!dname)
return -1;

- d_info->dname = zalloc(strlen(dname) + 1);
- if (!d_info->dname)
- return -1;
-
- d_info->dname = strdup(dname);
+ d_info->dname = dname;
}

cpumask = do_read_string(ff);
if (!cpumask)
return -1;

- d_info->cpumask = zalloc(strlen(cpumask) + 1);
- if (!d_info->cpumask)
- return -1;
- d_info->cpumask = strdup(cpumask);
+ d_info->cpumask = cpumask;

cpulist = do_read_string(ff);
if (!cpulist)
return -1;

- d_info->cpulist = zalloc(strlen(cpulist) + 1);
- if (!d_info->cpulist)
- return -1;
- d_info->cpulist = strdup(cpulist);
+ d_info->cpulist = cpulist;
}
}

--
2.52.0.457.g6b5491de43-goog