[Bug c/50581] stdarg doesn't support array types
joseph at codesourcery dot com
gcc-bugzilla@gcc.gnu.org
Fri Sep 30 20:13:00 GMT 2011
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50581
--- Comment #1 from joseph at codesourcery dot com <joseph at codesourcery dot com> 2011-09-30 19:41:17 UTC ---
There is no possible valid use of passing arrays to va_arg.
In C99, it is never possible for an array to be passed by value to a
function because it will have decayed to a pointer before the call.
Thus, any execution of va_arg with such an argument type results in
undefined behavior, just like calling it with "char" or "float", and
generating a runtime abort (with a compile-time warning) might be
appropriate, as is done for "char" and "float".
In C90, it is technically possible to use va_arg in this case without
undefined behavior. The argument passed to the function would have to be
a non-lvalue array - for example, an array in a structure returned from
another function. The result of va_arg would itself be a non-lvalue
array, which it is not possible to convert to a pointer, so it is not
possible to access the values in the array in any way; all that can be
done is to discard the value (call va_arg for its side effects) or to pass
it to another variadic function.
As far as I know this obscure C90-only use case works correctly (so you
can access arguments after the non-lvalue array using va_arg again, for
example). If you have problems with it, please provide a bug report with
a valid C90 testcase.
E.g.,
#include <stdarg.h>
typedef char array[1];
struct s { array a; };
struct s f (void);
void g (int a, ...);
void
h (void)
{
g (0, f().a);
}
void
g (int a, ...)
{
va_list ap;
va_start (ap, a);
va_arg (ap, array);
va_end (ap);
}
More information about the Gcc-bugs
mailing list