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

Roland Illig <[email protected]>
Newsgroups gmane.os.netbsd.devel.toolchain
Message-ID <[email protected]>
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;
}
~~~

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.

Roland
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.