[PATCH] perf data: fix inverted strncmp() in has_kcore_dir()
From: Gu Bin
Date: Tue Nov 03 2026 - 12:29:07 EST
has_kcore_dir() scans the perf.data output directory for a "kcore_dir"
(or guest "kcore_dir__*") entry and reports the match in `result`:
result = d ? strncmp(d->d_name, name, n) : false;
strncmp() returns 0 on a match, which coerces to boolean false, so the
assignment is inverted: `result` is set to true on a non-matching entry
and false on a matching one. Consequently the loop stops at the first
non-matching entry, causing the function to incorrectly report a
kcore_dir for any non-empty directory even if none exists. Only an
empty directory or a failed opendir() returns false correctly.
Both callers in perf inject treat the return value as "the input has a
kcore_dir": they enable copy_kcore_dir and mark the output as a
directory, so for an input directory without kcore_dir the copy is
attempted from a source that is not there.
Negate the strncmp() result so a matching entry sets result to true
and the scan continues until a match is found or the directory is
exhausted.
The prefix matching is deliberately left unchanged: comparing only the
first n bytes also matches guest kcore_dir__<pid> entries, which is
what the fixed commit intended, and matches the kcore_dir* glob that
copy_kcore_dir() copies.
Fixes: 65691e9ff0c9 ("perf tools: Make has_kcore_dir() work also for guest kcore_dir")
Signed-off-by: Gu Bin <gubin@xxxxxxxxxxxxxxxxxxxx>
---
tools/perf/util/data.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/perf/util/data.c b/tools/perf/util/data.c
index 33fd1b82019e..d4948e1d0633 100644
--- a/tools/perf/util/data.c
+++ b/tools/perf/util/data.c
@@ -531,7 +531,7 @@ bool has_kcore_dir(const char *path)
if (dir) {
while (d && !result) {
d = readdir(dir);
- result = d ? strncmp(d->d_name, name, n) : false;
+ result = d ? !strncmp(d->d_name, name, n) : false;
}
closedir(dir);
}
--
2.55.0.windows.3