Skip to contentSkip to conversation

selftests: kselftest_harness: fix Clang warning about zero-length format

PATCH Applied Jakub Kicinski 4 participants 4 messages Apr 16, 2024 – Apr 18, 2024

Subsystems (4)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

PATCH 1/1 selftests: kselftest_harness: fix Clang warning about zero-length format

Apparently it's more legal to pass the format as NULL, than it is to use an empty string. Clang complains about empty formats:

./../kselftest_harness.h:1207:30: warning: format string is empty [-Wformat-zero-length]
1207 | diagnostic ? "%s" : "", diagnostic);
| ^~
1 warning generated.

Fixes 378193eff339 ("selftests: kselftest_harness: let PASS / FAIL provide diagnostic")

---
---
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]

---
 tools/testing/selftests/kselftest.h         | 10 ++++++----
 tools/testing/selftests/kselftest_harness.h |  2 +-
 2 files changed, 7 insertions(+), 5 deletions(-)
tools/testing/selftests/kselftest.h +6 -4
index 541bf192e30e..4eca3fd1292c 100644
@@ -288,15 +288,17 @@ void ksft_test_result_code(int exit_code, const char *test_name,
 	}
 
 	/* Docs seem to call for double space if directive is absent */
-	if (!directive[0] && msg[0])
+	if (!directive[0] && msg)
 		directive = " #  ";
 
-	va_start(args, msg);
 	printf("%s %u %s%s", tap_code, ksft_test_num(), test_name, directive);
 	errno = saved_errno;
-	vprintf(msg, args);
+	if (msg) {
+		va_start(args, msg);
+		vprintf(msg, args);
+		va_end(args);
+	}
 	printf("\n");
-	va_end(args);
 }
 
 static inline int ksft_exit_pass(void)
tools/testing/selftests/kselftest_harness.h +1 -1
index 4fd735e48ee7..adb15cae79ab 100644
@@ -1202,7 +1202,7 @@ void __run_test(struct __fixture_metadata *f,
 		diagnostic = "unknown";
 
 	ksft_test_result_code(t->exit_code, test_name,
-			      diagnostic ? "%s" : "", diagnostic);
+			      diagnostic ? "%s" : NULL, diagnostic);
 }
 
 static int test_harness_run(int argc, char **argv)
Signature · 1 line
2.44.0
Tested-by on v1 1/1
#1 lore raw
Quoted 10 lines · Jakub Kicinski

Apparently it's more legal to pass the format as NULL, than it is to use an empty string. Clang complains about empty formats:

./../kselftest_harness.h:1207:30: warning: format string is empty [-Wformat-zero-length]
1207 | diagnostic ? "%s" : "", diagnostic);
| ^~
1 warning generated.

Quoted 4 lines
Fixes 378193eff339 ("selftests: kselftest_harness: let PASS / FAIL provide diagnostic")

Thanks Jakub!

Reviewed-by on v1 1/1
#2 lore raw
In reply to Jakub Kicinski
Quoted 10 lines · Jakub Kicinski

Apparently it's more legal to pass the format as NULL, than it is to use an empty string. Clang complains about empty formats:

./../kselftest_harness.h:1207:30: warning: format string is empty [-Wformat-zero-length]
1207 | diagnostic ? "%s" : "", diagnostic);
| ^~
1 warning generated.

Quoted 4 lines
Fixes 378193eff339 ("selftests: kselftest_harness: let PASS / FAIL provide diagnostic")
Quoted 51 lines

---
---
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]

---
 tools/testing/selftests/kselftest.h         | 10 ++++++----
 tools/testing/selftests/kselftest_harness.h |  2 +-
 2 files changed, 7 insertions(+), 5 deletions(-)
tools/testing/selftests/kselftest.h +6 -4
index 541bf192e30e..4eca3fd1292c 100644
@@ -288,15 +288,17 @@ void ksft_test_result_code(int exit_code, const char *test_name,
 	}
 
 	/* Docs seem to call for double space if directive is absent */
-	if (!directive[0] && msg[0])
+	if (!directive[0] && msg)
 		directive = " #  ";
 
-	va_start(args, msg);
 	printf("%s %u %s%s", tap_code, ksft_test_num(), test_name, directive);
 	errno = saved_errno;
-	vprintf(msg, args);
+	if (msg) {
+		va_start(args, msg);
+		vprintf(msg, args);
+		va_end(args);
+	}
 	printf("\n");
-	va_end(args);
 }
 
 static inline int ksft_exit_pass(void)
tools/testing/selftests/kselftest_harness.h +1 -1
index 4fd735e48ee7..adb15cae79ab 100644
@@ -1202,7 +1202,7 @@ void __run_test(struct __fixture_metadata *f,
 		diagnostic = "unknown";
 
 	ksft_test_result_code(t->exit_code, test_name,
-			      diagnostic ? "%s" : "", diagnostic);
+			      diagnostic ? "%s" : NULL, diagnostic);
 }
 
 static int test_harness_run(int argc, char **argv)
Signature · 2 lines
BR,
Muhammad Usama Anjum
#3 lore raw
In reply to Jakub Kicinski
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 11 lines · On Tue, 16 Apr 2024 08:10:48 -0700 you wrote:

Apparently it's more legal to pass the format as NULL, than it is to use an empty string. Clang complains about empty formats:

./../kselftest_harness.h:1207:30: warning: format string is empty [-Wformat-zero-length]
1207 | diagnostic ? "%s" : "", diagnostic);
| ^~
1 warning generated.

[...]

Here is the summary with links:
- [net] selftests: kselftest_harness: fix Clang warning about zero-length format

    https://git.kernel.org/netdev/net/c/caed8eba2215

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html