[PATCH v1 03/26] perf stat: Fix per_pkg_mask key leak on hashmap__add failure

From: Ian Rogers

Date: Fri Nov 13 2026 - 19:35:27 EST


In check_per_pkg(), key is allocated with malloc() before checking
whether the (die, socket) pair is already present in per_pkg_mask. If
hashmap__find() misses and hashmap__add() fails (e.g. -ENOMEM), key is
never freed.

Free key when hashmap__add() fails.

Fixes: 034f7ee130c1 ("perf stat: Fix wrong skipping for per-die aggregation")
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/stat.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/stat.c b/tools/perf/util/stat.c
index 6da3dbbde0e2..e7013449271d 100644
--- a/tools/perf/util/stat.c
+++ b/tools/perf/util/stat.c
@@ -363,8 +363,11 @@ static int check_per_pkg(struct evsel *counter, struct perf_counts_values *vals,
if (hashmap__find(mask, key, NULL)) {
*skip = true;
free(key);
- } else
+ } else {
ret = hashmap__add(mask, key, 1);
+ if (ret)
+ free(key);
+ }

return ret;
}
--
2.56.1.703.gf848ed807c-goog