Re: [PATCH] printf: add __printf attribute
From: Tamir Duberstein
Date: Sat Dec 06 2025 - 12:53:31 EST
On Sat, Dec 6, 2025 at 12:49 PM Andy Shevchenko
<andriy.shevchenko@xxxxxxxxxxxxxxx> wrote:
>
> On Sat, Dec 06, 2025 at 12:13:34PM -0500, Tamir Duberstein wrote:
> > On Sat, Dec 6, 2025 at 11:11 AM Andy Shevchenko
> > <andriy.shevchenko@xxxxxxxxxxxxxxx> wrote:
> > > On Sat, Dec 06, 2025 at 08:19:09AM -0500, Tamir Duberstein wrote:
>
> ...
>
> > > > -static void
> > > > +static void __printf(2, 3)
> > >
> > > 3?!
> > >
> > > I think it should be (2, 0). Yes, the both users call it with "%p..." in format
> > > string, but the second parameter tells compiler to check the variadic
> > > arguments, which are absent here. Changing 'const void *p' to '...' will align
> > > it with the given __printf() attribute, but I don't know if this what we want.
> >
> > The second parameter is the first-to-check, it is not specific to
> > variadic arguments. Using 0 means that no arguments are checkable, so
> > the compiler only validates the format string itself and won’t
> > diagnose mismatches with `p`. This works whether or not we later
> > change `const void *p` to `...`.
>
> Yes, but this is fragile. As I explained it works only because we supply
> the format string stuck to "%p", anything else will require reconsidering
> the function prototypes. So, strictly speaking this should be (2, 0) if
> we leave const void *p as is.
>
> --
> With Best Regards,
> Andy Shevchenko
I believe this is not correct. As I said, 0 means "do not check
arguments" so only the format string will be checked. See the existing
uses of this annotation in this file:
static void __printf(7, 0)
do_test(struct kunit *kunittest, const char *file, const int line, int
bufsize, const char *expect,
int elen, const char *fmt, va_list ap)
and
static void __printf(6, 7)
__test(struct kunit *kunittest, const char *file, const int line,
const char *expect, int elen,
const char *fmt, ...)
as you can see, 0 is used only when the arguments are not in the
function prototype at all. When variadic arguments are present, N+1 is
used.
Cheers.
Tamir