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 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