Skip to contentSkip to conversation

printf: convert test_hashed into macro

PATCH In review Tamir Duberstein 3 participants 6 messages Jan 21 – Jan 28 Vsprintf

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 printf: convert test_hashed into macro

This allows the compiler to check the arguments against the __printf() attribute on __test(). This produces better diagnostics when incorrect inputs are passed.

---
Changes in v3:
- Added parentheses to function references.
- Changed escaped newline indentation to tabs.
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Convert test_hashed to macro rather than try to annotate it in a way
that is compatible with both clang and gcc.
- Link to v1: https://patch.msgid.link/[email protected]

---
 lib/tests/printf_kunit.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
lib/tests/printf_kunit.c +11 -9
index 7617e5b8b02c..f6f21b445ece 100644
@@ -266,15 +266,17 @@ hash_pointer(struct kunit *kunittest)
 	KUNIT_EXPECT_MEMNEQ(kunittest, buf, PTR_STR, PTR_WIDTH);
 }
 
-static void
-test_hashed(struct kunit *kunittest, const char *fmt, const void *p)
-{
-	char buf[PLAIN_BUF_SIZE];
-
-	plain_hash_to_buffer(kunittest, p, buf, PLAIN_BUF_SIZE);
-
-	test(buf, fmt, p);
-}
+/*
+ * This is a macro so that the compiler can compare its arguments to the
+ * __printf() attribute on __test(). This cannot be a function with a __printf()
+ * attribute because GCC requires __printf() functions to be variadic.
+ */
+#define test_hashed(kunittest, fmt, p)						\
+	do {									\
+		char buf[PLAIN_BUF_SIZE];					\
+		plain_hash_to_buffer(kunittest, p, buf, PLAIN_BUF_SIZE);	\
+		test(buf, fmt, p);						\
+	} while (0)
 
 /*
  * NULL pointers aren't hashed.

---
base-commit: 24d479d26b25bce5faea3ddd9fa8f3a6c3129ea7
change-id: 20251206-printf-kunit-printf-attr-19369fc57bf0

Best regards,

Signature · 1 line
Tamir Duberstein <[email protected]>
reviewer Reviewed-by on v3 1/1
#1 lore raw
Quoted 3 lines · Tamir Duberstein

This allows the compiler to check the arguments against the __printf() attribute on __test(). This produces better diagnostics when incorrect inputs are passed.

Seems there is a consensus about this version, FWIW,

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 8 lines · Andy Shevchenko
Quoted 3 lines · Tamir Duberstein

This allows the compiler to check the arguments against the __printf() attribute on __test(). This produces better diagnostics when incorrect inputs are passed.

Seems there is a consensus about this version, FWIW,

Thanks Andy. Anything further required before this can be taken through printk?

Cheers,
Tamir

maintainer Reviewed-by on v3 1/1
#3 lore raw
In reply to Tamir Duberstein
Quoted 7 lines · Tamir Duberstein

This allows the compiler to check the arguments against the __printf() attribute on __test(). This produces better diagnostics when incorrect inputs are passed.

Looks good to me:

Best Regards,
Petr

PS: I am going to push it right now. It took me some time to get back
to this mail. I am a bit overloaded at the moment.

maintainer
#4 lore raw
In reply to Tamir Duberstein
Quoted 7 lines · Tamir Duberstein

This allows the compiler to check the arguments against the __printf() attribute on __test(). This produces better diagnostics when incorrect inputs are passed.

JFYI, the patch has been comitted into printk/linux.git, branch for-6.20.

Best Regards,
Petr

#5 lore raw
Quoted 10 lines · Petr Mladek
Quoted 7 lines · Tamir Duberstein

This allows the compiler to check the arguments against the __printf() attribute on __test(). This produces better diagnostics when incorrect inputs are passed.

Quoted 5 lines

JFYI, the patch has been comitted into printk/linux.git, branch for-6.20.

Best Regards,
Petr

Thanks all!
Tamir