All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] selftests/kvm: check fopen() return in get_run_delay()
@ 2026-11-04  8:07 zhangchujun
  2026-11-04  8:13 ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: zhangchujun @ 2026-11-04  8:07 UTC (permalink / raw)
  To: seanjc; +Cc: kvm, linux-kselftest, zhangchujun

get_run_delay() passes the FILE pointer returned by fopen() to
fscanf() and fclose() without checking it for NULL. If the
schedstat file cannot be opened, the program dereferences NULL
and crashes.

The comment above the fscanf() call already says to return
MIN_RUN_DELAY_NS upon failure just to be safe, so do exactly
that when fopen() fails.

Signed-off-by: zhangchujun <zhangchujun@cmss.chinamobile.com>

diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c
index f5f7189f96a8..2827f7d7d905 100644
--- a/tools/testing/selftests/kvm/lib/test_util.c
+++ b/tools/testing/selftests/kvm/lib/test_util.c
@@ -421,6 +421,8 @@ long get_run_delay(void)
 	sprintf(path, "/proc/%ld/schedstat", (long)kvm_gettid());
 	fp = fopen(path, "r");
 	/* Return MIN_RUN_DELAY_NS upon failure just to be safe */
+	if (!fp)
+		return MIN_RUN_DELAY_NS;
 	if (fscanf(fp, "%ld %ld ", &val[0], &val[1]) < 2)
 		val[1] = MIN_RUN_DELAY_NS;
 	fclose(fp);
-- 
2.50.1.windows.1




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

end of thread, other threads:[~2026-11-04 16:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-04  8:07 [PATCH] selftests/kvm: check fopen() return in get_run_delay() zhangchujun
2026-11-04  8:13 ` sashiko-bot
2026-11-04 16:50   ` Sean Christopherson

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.