public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] printf: convert test_hashed into macro
@ 2026-01-16 16:27 Tamir Duberstein
  2026-01-19  8:21 ` Andy Shevchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Tamir Duberstein @ 2026-01-16 16:27 UTC (permalink / raw)
  To: Petr Mladek, Steven Rostedt, Andy Shevchenko, Rasmus Villemoes,
	Sergey Senozhatsky, Nathan Chancellor, Nick Desaulniers,
	Bill Wendling, Justin Stitt
  Cc: Kees Cook, linux-kernel, oe-kbuild-all, llvm, kernel test robot,
	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.

Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202512061600.89CKQ3ag-lkp@intel.com/
Signed-off-by: Tamir Duberstein <tamird@kernel.org>
---
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/20251206-printf-kunit-printf-attr-v1-1-1682808b51d0@gmail.com
---
 lib/tests/printf_kunit.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/lib/tests/printf_kunit.c b/lib/tests/printf_kunit.c
index 7617e5b8b02c..1d96cea8af65 100644
--- a/lib/tests/printf_kunit.c
+++ b/lib/tests/printf_kunit.c
@@ -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: 983d014aafb14ee5e4915465bf8948e8f3a723b5
change-id: 20251206-printf-kunit-printf-attr-19369fc57bf0

Best regards,
--  
Tamir Duberstein <tamird@kernel.org>


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] printf: convert test_hashed into macro
  2026-01-16 16:27 [PATCH v2] printf: convert test_hashed into macro Tamir Duberstein
@ 2026-01-19  8:21 ` Andy Shevchenko
  2026-01-20 15:50   ` Tamir Duberstein
  0 siblings, 1 reply; 4+ messages in thread
From: Andy Shevchenko @ 2026-01-19  8:21 UTC (permalink / raw)
  To: Tamir Duberstein
  Cc: Petr Mladek, Steven Rostedt, Rasmus Villemoes, Sergey Senozhatsky,
	Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt,
	Kees Cook, linux-kernel, oe-kbuild-all, llvm, kernel test robot

On Fri, Jan 16, 2026 at 11:27:03AM -0500, Tamir Duberstein wrote:
> This allows the compiler to check the arguments against the __printf

__printf() since it takes parameters, OTOH it's an attribute at the end,
so I have no strong opinion on how to spell it.

> attribute on __test. This produces better diagnostics when incorrect

__test()

*This is reference to a function.

> inputs are passed.

...

> +/*
> + * 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.

As per commit message remarks.

> + */
> +#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)

Make sure you used tabs to indent the \:s.

...

The downside of a macro is a killing of compile-time type checks.

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] printf: convert test_hashed into macro
  2026-01-19  8:21 ` Andy Shevchenko
@ 2026-01-20 15:50   ` Tamir Duberstein
  2026-01-21 16:07     ` Petr Mladek
  0 siblings, 1 reply; 4+ messages in thread
From: Tamir Duberstein @ 2026-01-20 15:50 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Petr Mladek, Steven Rostedt, Rasmus Villemoes, Sergey Senozhatsky,
	Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt,
	Kees Cook, linux-kernel, oe-kbuild-all, llvm, kernel test robot

On Mon, Jan 19, 2026 at 3:21 AM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> On Fri, Jan 16, 2026 at 11:27:03AM -0500, Tamir Duberstein wrote:
> > This allows the compiler to check the arguments against the __printf
>
> __printf() since it takes parameters, OTOH it's an attribute at the end,
> so I have no strong opinion on how to spell it.
>
> > attribute on __test. This produces better diagnostics when incorrect
>
> __test()
>
> *This is reference to a function.
>
> > inputs are passed.
>
> ...
>
> > +/*
> > + * 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.
>
> As per commit message remarks.
>
> > + */
> > +#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)
>
> Make sure you used tabs to indent the \:s.
>
> ...
>
> The downside of a macro is a killing of compile-time type checks.

This macro still bottoms out in __test() which is a function, so I
believe it preserves (and enhances, per the commit message)
compile-time checks.

I'll send v3 with the changes you requested above.

>
> --
> With Best Regards,
> Andy Shevchenko
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] printf: convert test_hashed into macro
  2026-01-20 15:50   ` Tamir Duberstein
@ 2026-01-21 16:07     ` Petr Mladek
  0 siblings, 0 replies; 4+ messages in thread
From: Petr Mladek @ 2026-01-21 16:07 UTC (permalink / raw)
  To: Tamir Duberstein
  Cc: Andy Shevchenko, Steven Rostedt, Rasmus Villemoes,
	Sergey Senozhatsky, Nathan Chancellor, Nick Desaulniers,
	Bill Wendling, Justin Stitt, Kees Cook, linux-kernel,
	oe-kbuild-all, llvm, kernel test robot

On Tue 2026-01-20 10:50:09, Tamir Duberstein wrote:
> On Mon, Jan 19, 2026 at 3:21 AM Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:
> >
> > On Fri, Jan 16, 2026 at 11:27:03AM -0500, Tamir Duberstein wrote:
> > > This allows the compiler to check the arguments against the __printf
> >
> > __printf() since it takes parameters, OTOH it's an attribute at the end,
> > so I have no strong opinion on how to spell it.
> >
> > > attribute on __test. This produces better diagnostics when incorrect
> >
> > __test()
> >
> > *This is reference to a function.
> >
> > > inputs are passed.
> >
> > ...
> >
> > > +/*
> > > + * 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.
> >
> > As per commit message remarks.
> >
> > > + */
> > > +#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)
> >
> > Make sure you used tabs to indent the \:s.
> >
> > ...
> >
> > The downside of a macro is a killing of compile-time type checks.
> 
> This macro still bottoms out in __test() which is a function, so I
> believe it preserves (and enhances, per the commit message)
> compile-time checks.
> 
> I'll send v3 with the changes you requested above.

JFYI, I am fine with this approach.

Best Regards,
Petr

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-01-21 16:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-16 16:27 [PATCH v2] printf: convert test_hashed into macro Tamir Duberstein
2026-01-19  8:21 ` Andy Shevchenko
2026-01-20 15:50   ` Tamir Duberstein
2026-01-21 16:07     ` Petr Mladek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox