Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question about va_* macros

7 views
Skip to first unread message

Juan Manuel Guerrero

unread,
Dec 21, 2012, 10:37:42 AM12/21/12
to dj...@delorie.com
I am interesting in recognize the end of the argument list of functions like
printf and scanf when they are accessed using va_* macros. I have traversed
the list (stack) using va_arg and observed that the list seems to end with a
NULL pointer. The question arises if the finalizing of the list with a NULL
pointer can be taken for granted or not. Please note that I am aware that
info doc of the va_* macros tells that the user must provide a mechanism to
determinate the end of the list usually either by a NULL pointer or by some
counter or something else. I am especially interested in how it works for
_doprnt and _doscan.

TIA

Juan M. Guerrero

DJ Delorie

unread,
Dec 21, 2012, 1:42:06 PM12/21/12
to dj...@delorie.com

You can't rely on a NULL at the end of a va_list.

I have seen *many* bugs where a missing explicit NULL in a va call
caused problems because there was no terminator.

Juan Manuel Guerrero

unread,
Dec 21, 2012, 7:52:16 PM12/21/12
to dj...@delorie.com
Disapointing.
The reason why I have asked this is because according to C99 or POSIX
printf and scanf family of functions shall return EINVAL if there are
insufficient arguments. This makes it necessary to be able to detect
how many arguments are in va_list. I have single stepped through the
_doscan() code inspecting the content of va_list and the list was always
terminated with a NULL pointer. I only checked this with gcc472b.
Because I have seen no djgpp specific code that added the list terminator
I have assumed that this was done by gcc and that it could be taken for
granted that the list will always be terminated this way.
I will will look at glibc and cygwin to see how they handle this issue.

Regards,
Juan M. Guerrero

Hans-Bernhard Bröker

unread,
Dec 21, 2012, 8:54:36 PM12/21/12
to
On 22.12.2012 01:52, Juan Manuel Guerrero wrote:

> The reason why I have asked this is because according to C99 or POSIX
> printf and scanf family of functions shall return EINVAL if there are
> insufficient arguments.

At least for C99 itself, I'm pretty sure there cannot be any such
requirment. The term EINVAL is not mentioned at all in C99, nor C11 for
that matter. What both C99 and C11 actually say, for both fscanf and
fprint, is:

> If there are insufficient arguments for the format, the behavior is
> undefined.

Juan Manuel Guerrero

unread,
Dec 22, 2012, 10:03:43 AM12/22/12
to
True. It is only an X/Open System Interface (XSI) extension.
0 new messages