vsnprintf and friends modify the va_list argument

Doug Hardie bc979 at lafn.org
Sun Nov 16 22:34:25 UTC 2014

I suspect this needs to be in a WARNING section of the printf(3) man page.  The print functions that use variable arguments modify the argument list so that only one can be called.  For example, to print the arguments and also write them to a file using the following code will cause a segmentation violation:

vprintf (fd, fmt, args);
vsnprintf (buf, sizeof(buf), fmt, args);

Switching the order of the calls doesn’t eliminate the segmentation violation.  You have to do something on the order of:

vsnprintf (buf, sizeof(buf), fmt, args);
fprintf (fd, “%s”, buf);

More information about the freebsd-questions mailing list