[PATCH v7 01/10] perf kmem: Fix alloc_func_regex leak on error in build_alloc_func_list()
From: Ian Rogers
Date: Sat Nov 21 2026 - 02:17:18 EST
In build_alloc_func_list(), after regcomp(&alloc_func_regex, ...)
succeeds, the early error returns on map__load() failure and realloc()
failure return without calling regfree(&alloc_func_regex), leaking the
compiled regular expression buffer.
Call regfree(&alloc_func_regex) on both error paths before returning.
Fixes: c9758cc45699 ("perf kmem: Implement stat --page --caller")
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/builtin-kmem.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index 8693c6b135ca..1404b523a785 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -364,6 +364,7 @@ static int build_alloc_func_list(void)
kernel_map = machine__kernel_map(machine);
if (map__load(kernel_map) < 0) {
pr_err("cannot load kernel map\n");
+ regfree(&alloc_func_regex);
return -ENOENT;
}
@@ -373,8 +374,10 @@ static int build_alloc_func_list(void)
func = realloc(alloc_func_list,
(nr_alloc_funcs + 1) * sizeof(*func));
- if (func == NULL)
+ if (func == NULL) {
+ regfree(&alloc_func_regex);
return -ENOMEM;
+ }
pr_debug("alloc func: %s\n", sym->name);
func[nr_alloc_funcs].start = sym->start;
--
2.56.1.750.gf70dc682cf-goog