ping//Re: [PATCH -next 2/2] selftests: Fix fscanf warning

From: chenlifu
Date: Fri Aug 13 2021 - 03:27:46 EST




在 2021/7/9 14:37, Chen Lifu 写道:
Compile selftests occurs the following warnings:
make -C tools/testing/selftests
...

lib/test_util.c: In function ‘get_trans_hugepagesz’:
lib/test_util.c:138:2: warning: ignoring return value of ‘fscanf’, declared with attribute warn_unused_result [-Wunused-result]
138 | fscanf(f, "%ld", &size);
| ^~~~~~~~~~~~~~~~~~~~~~~
x86_64/mmio_warning_test.c: In function ‘get_warnings_count’:
x86_64/mmio_warning_test.c:85:2: warning: ignoring return value of ‘fscanf’, declared with attribute warn_unused_result [-Wunused-result]
85 | fscanf(f, "%d", &warnings);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
x86_64/xen_shinfo_test.c: In function ‘get_run_delay’:
x86_64/xen_shinfo_test.c:109:9: warning: ignoring return value of ‘fscanf’, declared with attribute warn_unused_result [-Wunused-result]
109 | fscanf(fp, "%ld %ld ", &val[0], &val[1]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
steal_time.c: In function ‘get_run_delay’:
steal_time.c:228:2: warning: ignoring return value of ‘fscanf’, declared with attribute warn_unused_result [-Wunused-result]
228 | fscanf(fp, "%ld %ld ", &val[0], &val[1]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Check the return value of ‘fscanf’ to fix it.

Signed-off-by: Chen Lifu <chenlifu@xxxxxxxxxx>
---
tools/testing/selftests/kvm/lib/test_util.c | 3 ++-
tools/testing/selftests/kvm/steal_time.c | 3 ++-
tools/testing/selftests/kvm/x86_64/mmio_warning_test.c | 3 ++-
tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c | 3 ++-
4 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c
index af1031fed97f..02ed0d5c3aa5 100644
--- a/tools/testing/selftests/kvm/lib/test_util.c
+++ b/tools/testing/selftests/kvm/lib/test_util.c
@@ -135,7 +135,8 @@ size_t get_trans_hugepagesz(void)
f = fopen("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size", "r");
TEST_ASSERT(f != NULL, "Error in opening transparent_hugepage/hpage_pmd_size");
- fscanf(f, "%ld", &size);
+ if (fscanf(f, "%ld", &size) != 1)
+ size = 0;
fclose(f);
return size;
diff --git a/tools/testing/selftests/kvm/steal_time.c b/tools/testing/selftests/kvm/steal_time.c
index b0031f2d38fd..18f231c45a12 100644
--- a/tools/testing/selftests/kvm/steal_time.c
+++ b/tools/testing/selftests/kvm/steal_time.c
@@ -225,7 +225,8 @@ static long get_run_delay(void)
sprintf(path, "/proc/%ld/schedstat", syscall(SYS_gettid));
fp = fopen(path, "r");
- fscanf(fp, "%ld %ld ", &val[0], &val[1]);
+ if (fscanf(fp, "%ld %ld ", &val[0], &val[1]) != 2)
+ val[1] = 0;
fclose(fp);
return val[1];
diff --git a/tools/testing/selftests/kvm/x86_64/mmio_warning_test.c b/tools/testing/selftests/kvm/x86_64/mmio_warning_test.c
index e6480fd5c4bd..e770037e5cc9 100644
--- a/tools/testing/selftests/kvm/x86_64/mmio_warning_test.c
+++ b/tools/testing/selftests/kvm/x86_64/mmio_warning_test.c
@@ -82,7 +82,8 @@ int get_warnings_count(void)
FILE *f;
f = popen("dmesg | grep \"WARNING:\" | wc -l", "r");
- fscanf(f, "%d", &warnings);
+ if (fscanf(f, "%d", &warnings) != 1)
+ warnings = 0;
fclose(f);
return warnings;
diff --git a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c
index 117bf49a3d79..6efda86083f1 100644
--- a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c
+++ b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c
@@ -106,7 +106,8 @@ static long get_run_delay(void)
sprintf(path, "/proc/%ld/schedstat", syscall(SYS_gettid));
fp = fopen(path, "r");
- fscanf(fp, "%ld %ld ", &val[0], &val[1]);
+ if (fscanf(fp, "%ld %ld ", &val[0], &val[1]) != 2)
+ val[1] = 0;
fclose(fp);
return val[1];