Linux Perf Users
 help / color / mirror / Atom feed
* [PATCH] perf data: fix inverted strncmp() in has_kcore_dir()
@ 2026-11-03 17:11 Gu Bin
  2026-11-03 17:14 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Gu Bin @ 2026-11-03 17:11 UTC (permalink / raw)
  To: acme
  Cc: peterz, mingo, namhyung, mark.rutland, alexander.shishkin, jolsa,
	irogers, adrian.hunter, james.clark, linux-perf-users,
	linux-kernel, Gu Bin

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@cmss.chinamobile.com>
---
 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




^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] perf data: fix inverted strncmp() in has_kcore_dir()
  2026-11-03 17:11 [PATCH] perf data: fix inverted strncmp() in has_kcore_dir() Gu Bin
@ 2026-11-03 17:14 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-03 17:14 UTC (permalink / raw)
  To: Gu Bin; +Cc: linux-perf-users

> 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@cmss.chinamobile.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261103171127.1628-1-gubin@cmss.chinamobile.com?part=1


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-03 17:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-03 17:11 [PATCH] perf data: fix inverted strncmp() in has_kcore_dir() Gu Bin
2026-11-03 17:14 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox