tech-toolchain archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

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



Am 20.06.2023 um 15:45 schrieb tlaronde%polynum.com@localhost:
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



Home | Main Index | Thread Index | Old Index