This blog post claims that passing a va_list
to another function like in the following code is unsafe, and that the va_list
must first be copied using va_copy
:
void
foo_ap(const char *fmt, va_list ap)
{
char buf[128];
vsnprintf(buf, sizeof(buf), fmt, ap);
// now, do something with buf ...
}
Like one of the comments to the blog post mentions, I can't see how this would be unsafe, as the C99 spec states (7.15):
The object
ap
may be passed as an argument to another function; if that function invokes theva_arg
macro with parameterap
, the value ofap
in the calling function is indeterminate and shall be passed to theva_end
macro prior to any further reference toap
.
(As long as foo_ap
doesn't reference ap
after passing it on, the second part of the sentence doesn't seem to apply anyway.) The author of the blog post says in another comment that he might be wrong, but maybe someone can add some clarification. Do I really have to use va_copy
to be on the safe side? Or are there any platforms that don't conform to the spec and require the use of va_copy
?
You only need va_copy
if you still plan to use ap
after vsnprintf
returns. For example, you might want to call vsnprintf
twice - once to determine the required buffer size, and again to format for real. If you don't need to use ap
again (except perhaps for passing it to va_end
, if you were the one creating it with va_start
), then you don't need to va_copy
it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With