[Bug c/50581] New: stdarg doesn't support array types
Wolfgang at Solfrank dot net
gcc-bugzilla@gcc.gnu.org
Fri Sep 30 15:49:00 GMT 2011
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50581
Bug #: 50581
Summary: stdarg doesn't support array types
Classification: Unclassified
Product: gcc
Version: 4.5.3
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: Wolfgang@Solfrank.net
__builtin_va_arg doesn't work correctly if given an array type, as it doesn't
take the promotion of array to pointer into account. When called without some
-std=xxx argument, gcc complains with the message "invalid use of non-lvalue
array". If it is called with e.g. -std=c99, it doesn't complain, but generates
incorrect code nevertheless.
The problem can be reproduced by the following program:
-------------------------------------------
typedef char array[10];
array arr;
void
f(char *fmt, ...)
{
__builtin_va_list ap;
__builtin_va_start(ap, fmt);
__builtin_memcpy(arr, __builtin_va_arg(ap, array), sizeof arr);
__builtin_va_end(ap);
}
-------------------------------------------
Fixing this is especially useful for types the user doesn't even know whether
it is an array or not, like e.g. __builtin_va_list itself to allow for
recursive variadic argument passing:
-------------------------------------------
void
f(char *fmt, ...)
{
__builtin_va_list ap, ap1;
__builtin_va_start(ap, fmt);
__builtin_va_copy(ap1, __builtin_va_arg(ap, __builtin_va_list));
__builtin_va_end(ap);
__builtin_va_copy(ap, ap1);
__builtin_va_end(ap1);
__builtin_va_end(ap);
}
-------------------------------------------
The fix is quite easy. Just apply the following diff to gcc/c-common.c:
-------------------------------------------
Index: gcc/c-common.c
===================================================================
RCS file: /cvsroot/src/external/gpl3/gcc/dist/gcc/c-common.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 c-common.c
--- gcc/c-common.c 21 Jun 2011 01:20:03 -0000 1.1.1.1
+++ gcc/c-common.c 30 Sep 2011 15:12:23 -0000
@@ -5124,6 +5124,14 @@
tree
build_va_arg (location_t loc, tree expr, tree type)
{
+ /*
+ * Since arrays are converted to pointers when given as function arguments,
+ * here we have to do the same with the type of the argument.
+ * XXX Does it make sense to do the same with function type arguments?
+ */
+ if (TREE_CODE(type) == ARRAY_TYPE)
+ type = build_pointer_type(strip_array_types(type));
+
expr = build1 (VA_ARG_EXPR, type, expr);
SET_EXPR_LOCATION (expr, loc);
return expr;
-------------------------------------------
More information about the Gcc-bugs
mailing list