Quentin Pope <qp19433@hotmail.NOSPAM.com> writes:
> Consider the variadic function with the following prototype:
> int foo(int num,...);
> Here 'num' specifies the number of arguments, and assume that all the
> arguments that should be passed to this function are of type int.
> (My question has nothing to do with the definition of the function
> foo, so don't bother about it.)
>
> If I call the function as:
> foo(2,3,4,5,6,7,8);/*More arguments than expected*/
>
> Here I call foo with too many arguments than expected by its
> definition.
>
> Assuming that the prototype is visible in the scope of the function
> call and the definition of function foo itself does NOT produce any
> UB, please tell me if this function call produces UB or not.
>
> I am not able to find any explicit clause from C99 which deals with
> this behavior.
>
> Though I find that:
>
> 7.19.6.1 The fprintf function
> The fprintf function writes output to the stream pointed to by stream,
> under control of the string pointed to by format that specifies how
> subsequent arguments are converted for output. If there are
> insufficient arguments for the format, the behavior is undefined. If
> the format is exhausted while arguments remain, the excess arguments
> are evaluated (as always) but are otherwise ignored.
>
> This is highly specific to fprintf. I wonder if it applies to all
> variadic functions.
I believe the behavior is well defined.
The call itself cannot reasonably have undefined behavior, since foo()
itself *could* have called va_arg enough times to consume all the
arguments.
Inside foo, the only potentially problematic think you're doing is
invoking va_end() before consuming all the arguments. Invoking va_arg()
when there are no more arguments is UB (C99 7.15.11.1p), but that
doesn't apply here.
There's nothing in the description of va_end() that suggests that its
behavior depends on whether previous calls to va_arg() have consumed all
the arguments.
(The behavior of fprintf() is described explicitly because there's no
requirement for fprintf() to use the <stdarg.h> mechanism; legacy
implementations would have used something else, perhaps <varargs.h>,
and fprintf() could be implemented in non-portable C or in a language
other than C.)
[...]
>
> This email and any file attachments(s) are confidential, may be legally
> privileged, and are intended solely for use by the identified recipient
[29 lines deleted]
> responsibility for any loss or damage arising from the use of this email
> or attachments.
If you can find a way to post without including this nonsense, I'm sure
we'd all appreciated it.
--
Keith Thompson (The_Other_Keith)
kst-u@mib.org <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"