[PATCH v1 1/5] perf test: Fix a memory leak in PE file parsing test

From: Namhyung Kim

Date: Sun Oct 18 2026 - 12:40:44 EST


The get_argv_exec_path() returns a dynamically allocated string so it
should be freed. Let's check the return value first so that it
doesn't crash snprintf().

Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
Closes: https://lore.kernel.org/r/sashiko-outbox-168781@xxxxxxxxxx
Signed-off-by: Namhyung Kim <namhyung@xxxxxxxxxx>
---
tools/perf/tests/pe-file-parsing.c | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/tools/perf/tests/pe-file-parsing.c b/tools/perf/tests/pe-file-parsing.c
index 190520ffc3ad6d0c..ed95aa5d8ca2af92 100644
--- a/tools/perf/tests/pe-file-parsing.c
+++ b/tools/perf/tests/pe-file-parsing.c
@@ -74,19 +74,26 @@ static int test__pe_file_parsing(struct test_suite *test __maybe_unused,
{
struct stat st;
char path_dir[PATH_MAX];
- const char * const tests_dirs[] = {
+ static const char * const tests_dirs[] = {
"./tools/perf/tests",
"./tests",
"./source/tests",
- path_dir,
};
- snprintf(path_dir, PATH_MAX, "%s/tests", get_argv_exec_path());
+ char *exec_path;

for (size_t i = 0; i < ARRAY_SIZE(tests_dirs); ++i) {
if (!lstat(tests_dirs[i], &st))
return run_dir(tests_dirs[i]);
}

+ exec_path = get_argv_exec_path();
+ TEST_ASSERT_VAL("Failed to allocate exec_path", exec_path);
+ snprintf(path_dir, PATH_MAX, "%s/tests", exec_path);
+ free(exec_path);
+
+ if (!lstat(path_dir, &st))
+ return run_dir(path_dir);
+
pr_debug("cannot find the tests directory\n");
return TEST_SKIP;
}
--
2.56.0.176.g3575e65682-goog