[PATCH] selftests/net: fix unused return value warnings in ksft.h

Nai-Chen Cheng posted 1 patch 5 months ago
tools/testing/selftests/net/lib/ksft.h | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
[PATCH] selftests/net: fix unused return value warnings in ksft.h
Posted by Nai-Chen Cheng 5 months ago
The write() and read() system calls in ksft_ready() and ksft_wait()
functions return values that were not being checked, causing complier
warnings with GCC.

Fix the warnings by casting the return values to void to indicate that
ignoring them is intentional.

Signed-off-by: Nai-Chen Cheng <bleach1827@gmail.com>
---
 tools/testing/selftests/net/lib/ksft.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/net/lib/ksft.h b/tools/testing/selftests/net/lib/ksft.h
index 17dc34a612c64e549f634e82a23f317b2ff6a282..0ca2cb408c643bc76c0aaea684f0e7e28e6b05a6 100644
--- a/tools/testing/selftests/net/lib/ksft.h
+++ b/tools/testing/selftests/net/lib/ksft.h
@@ -10,6 +10,7 @@ static inline void ksft_ready(void)
 {
 	const char msg[7] = "ready\n";
 	char *env_str;
+	ssize_t ret;
 	int fd;
 
 	env_str = getenv("KSFT_READY_FD");
@@ -24,7 +25,8 @@ static inline void ksft_ready(void)
 		fd = STDOUT_FILENO;
 	}
 
-	write(fd, msg, sizeof(msg));
+	ret = write(fd, msg, sizeof(msg));
+	(void)ret;
 	if (fd != STDOUT_FILENO)
 		close(fd);
 }
@@ -33,6 +35,7 @@ static inline void ksft_wait(void)
 {
 	char *env_str;
 	char byte;
+	ssize_t ret;
 	int fd;
 
 	env_str = getenv("KSFT_WAIT_FD");
@@ -48,7 +51,8 @@ static inline void ksft_wait(void)
 		fd = STDIN_FILENO;
 	}
 
-	read(fd, &byte, sizeof(byte));
+	ret = read(fd, &byte, sizeof(byte));
+	(void)ret;
 	if (fd != STDIN_FILENO)
 		close(fd);
 }

---
base-commit: d1d10cea0895264cc3769e4d9719baa94f4b250b
change-id: 20250906-selftests-net-ksft-37266937bc4d

Best regards,
-- 
Nai-Chen Cheng <bleach1827@gmail.com>
Re: [PATCH] selftests/net: fix unused return value warnings in ksft.h
Posted by Jakub Kicinski 5 months ago
On Sat, 06 Sep 2025 23:59:28 +0800 Nai-Chen Cheng wrote:
> The write() and read() system calls in ksft_ready() and ksft_wait()
> functions return values that were not being checked, causing complier
> warnings with GCC.

Is it just a GCC warning or rather a combination of GCC and some
misguided glibc decorator to force check the return of read/write?
Naming the compiler versions and the warning flag which enables
this would be useful. We don't see it building with normal warning
level today.

> Fix the warnings by casting the return values to void to indicate that
> ignoring them is intentional.

> ret = read(fd, &byte, sizeof(byte));
> (void)ret;

Can you not cast the read() to void directly?
-- 
pw-bot: cr
Re: [PATCH] selftests/net: fix unused return value warnings in ksft.h
Posted by Nai-Chen(Simone) Cheng 5 months ago
On 2025-09-09 09:20, Jakub Kicinski wrote:
 > Is it just a GCC warning or rather a combination of GCC and some
 > misguided glibc decorator to force check the return of read/write?
 > Naming the compiler versions and the warning flag which enables
 > this would be useful. We don't see it building with normal warning
 > level today.

Thanks for the review!

I found that the warnings occur under specific build
conditions that explain why it's not consistently seen:

The warning appears when manually cleaning and rebuilding net/lib/:
   cd tools/testing/selftests/net/lib/
   make clean && make

The warning messages are:

ksft.h: In function ‘ksft_ready’:
ksft.h:27:9: warning: ignoring return value of ‘write’ declared with 
attribute ‘warn_unused_result’ [-Wunused-result]

ksft.h: In function ‘ksft_wait’:
ksft.h:51:9: warning: ignoring return value of ‘read’ declared with 
attribute ‘warn_unused_result’ [-Wunused-result]

This is triggered by:
- GCC version: 14.2.0
- -Wall flag (which includes -Wunused-result)

During investigation of this unused result warning and following up on 
the patch by Minh-Quang Bui [1], I also discovered an issue with the 
selftests build system: running 'make clean' from 
tools/testing/selftests/ doesn't clean objects in net/lib/ because the 
clean target doesn't include $(INSTALL_DEPS_TARGET). This explains why 
net/lib compiled objects persist after cleaning and why the warning only
appears with manual cleaning of that specific directory.

 > Can you not cast the read() to void directly?

Sure. Direct casting is much cleaner. I haven't noticed it... Thanks for 
the advice.

Would it be acceptable to:
1. Send a v2 of this patch with the direct void casting approach and 
more precise commit message?
2. Send a separate patch to fix the selftests Makefile clean target to 
include $(INSTALL_DEPS_TARGET)?

[1] 
https://lore.kernel.org/all/20250601142914.13379-1-minhquangbui99@gmail.com/

Thanks,
Nai-Chen Cheng
Re: [PATCH] selftests/net: fix unused return value warnings in ksft.h
Posted by Jakub Kicinski 5 months ago
On Tue, 9 Sep 2025 18:00:17 +0800 Nai-Chen(Simone) Cheng wrote:
> Would it be acceptable to:
> 1. Send a v2 of this patch with the direct void casting approach and 
> more precise commit message?
> 2. Send a separate patch to fix the selftests Makefile clean target to 
> include $(INSTALL_DEPS_TARGET)?

Sounds good!
Re: [PATCH] selftests/net: fix unused return value warnings in ksft.h
Posted by Nai-Chen(Simone) Cheng 4 months, 4 weeks ago
Hi Jakub,

On 2025-09-10 07:25, Jakub Kicinski wrote:
> Sounds good!

I tried the direct void casting approach, but it still generates 
warnings with GCC 14.2.0:
     (void)write(fd, msg, sizeof(msg));
     still shows: warning: ignoring return value of 'write'...

After further researching, I found this appears to be a known GCC/glibc 
issue dating back to 2015 where direct void casting doesn't work for 
functions with __warn_unused_result__. [1] Since this is a long-standing 
toolchain issue and direct void casting won't suppress warning, I think 
you can skip this patch. Thank you!

[1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425

Best Regards,
Nai-Chen Cheng