From: Tamir Duberstein <tamird@kernel.org>
To: Petr Mladek <pmladek@suse.com>,
Steven Rostedt <rostedt@goodmis.org>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Sergey Senozhatsky <senozhatsky@chromium.org>,
Nathan Chancellor <nathan@kernel.org>,
Nick Desaulniers <nick.desaulniers+lkml@gmail.com>,
Bill Wendling <morbo@google.com>,
Justin Stitt <justinstitt@google.com>
Cc: Kees Cook <kees@kernel.org>,
linux-kernel@vger.kernel.org, oe-kbuild-all@lists.linux.dev,
llvm@lists.linux.dev, kernel test robot <lkp@intel.com>,
Tamir Duberstein <tamird@kernel.org>
Subject: [PATCH v2] printf: convert test_hashed into macro
Date: Fri, 16 Jan 2026 11:27:03 -0500 [thread overview]
Message-ID: <20260116-printf-kunit-printf-attr-v2-1-3dd7fefc3207@kernel.org> (raw)
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>
next reply other threads:[~2026-01-16 16:27 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-16 16:27 Tamir Duberstein [this message]
2026-01-19 8:21 ` [PATCH v2] printf: convert test_hashed into macro Andy Shevchenko
2026-01-20 15:50 ` Tamir Duberstein
2026-01-21 16:07 ` Petr Mladek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260116-printf-kunit-printf-attr-v2-1-3dd7fefc3207@kernel.org \
--to=tamird@kernel.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=justinstitt@google.com \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=lkp@intel.com \
--cc=llvm@lists.linux.dev \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=nick.desaulniers+lkml@gmail.com \
--cc=oe-kbuild-all@lists.linux.dev \
--cc=pmladek@suse.com \
--cc=rostedt@goodmis.org \
--cc=senozhatsky@chromium.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.