Skip to content

Commit 9bfa52d

Browse files
tamirdpmladek
authored andcommitted
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. 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> Reviewed-by: Petr Mladek <pmladek@suse.com> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Link: https://patch.msgid.link/20260121-printf-kunit-printf-attr-v3-1-4144f337ec8b@kernel.org Signed-off-by: Petr Mladek <pmladek@suse.com>
1 parent bdfcca6 commit 9bfa52d

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

lib/tests/printf_kunit.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,17 @@ hash_pointer(struct kunit *kunittest)
266266
KUNIT_EXPECT_MEMNEQ(kunittest, buf, PTR_STR, PTR_WIDTH);
267267
}
268268

269-
static void
270-
test_hashed(struct kunit *kunittest, const char *fmt, const void *p)
271-
{
272-
char buf[PLAIN_BUF_SIZE];
273-
274-
plain_hash_to_buffer(kunittest, p, buf, PLAIN_BUF_SIZE);
275-
276-
test(buf, fmt, p);
277-
}
269+
/*
270+
* This is a macro so that the compiler can compare its arguments to the
271+
* __printf() attribute on __test(). This cannot be a function with a __printf()
272+
* attribute because GCC requires __printf() functions to be variadic.
273+
*/
274+
#define test_hashed(kunittest, fmt, p) \
275+
do { \
276+
char buf[PLAIN_BUF_SIZE]; \
277+
plain_hash_to_buffer(kunittest, p, buf, PLAIN_BUF_SIZE); \
278+
test(buf, fmt, p); \
279+
} while (0)
278280

279281
/*
280282
* NULL pointers aren't hashed.

0 commit comments

Comments
 (0)