Re: WARNS > 3 then -Wformat=2: pb with va_list

[email protected]
Newsgroups gmane.os.netbsd.devel.toolchain
Message-ID <[email protected]>
Le Tue, Jun 20, 2023 at 05:34:19PM +0200, Roland Illig a écrit :
> Am 20.06.2023 um 15:45 schrieb [email protected]:
> > Is there a way to please the compiler when using a va_list and calling
> > syslog(3) or fprintf(3)?
> 
> When you forward to syslog, you have to call vsyslog(3), not syslog(3).
> 
> ~~~c
> #include <stdarg.h>
> #include <stdio.h>
> #include <syslog.h>
> 
> static void __sysloglike(2, 3)
> my_syslog(int prio, const char *fmt, ...)
> {
> 	va_list ap;
> 
> 	va_start(ap, fmt);
> 	vfprintf(stderr, fmt, ap);
> 	va_end(ap);
> 	fprintf(stderr, "\n");
> 
> 	va_start(ap, fmt);
> 	vsyslog(prio, fmt, ap);
> 	va_end(ap);
> }
> 
> int
> main(int argc, char **argv)
> {
> 	my_syslog(1, "%d", 4);
> 	my_syslog(2, "%.*s", 5, "hello, world");
> 	return 0;
> }

OK, thanks!

> ~~~
> 
> When you change the 'vsyslog' to plain 'syslog', you get:
> 
> > sl.c:16:2: error: format not a string literal,
> >     argument types not checked [-Werror=format-nonliteral]
> 
> This error message is highly confusing, it should rather say that
> 'syslog' takes varargs, not a 'va_list'.
> 
> Curiously, GCC 10 doesn't complain when I pass a __sysloglike string to
> a printf-like function, and vice versa.

I think this is due to the definition of __sysloglike in cdefs.h: it is
in fact an alias to __printflike if __syslog_attribute__ is not defined.
-- 
        Thierry Laronde <tlaronde +AT+ polynum +dot+ com>
                     http://www.kergis.com/
                    http://kertex.kergis.com/
Key fingerprint = 0FF7 E906 FBAF FE95 FD89  250D 52B1 AE95 6006 F40C
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.