public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/50581] New: stdarg doesn't support array types
@ 2011-09-30 15:49 Wolfgang at Solfrank dot net
  2011-09-30 20:13 ` [Bug c/50581] " joseph at codesourcery dot com
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: Wolfgang at Solfrank dot net @ 2011-09-30 15:49 UTC (permalink / raw)
  To: gcc-bugs

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;
-------------------------------------------


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-01-03  2:38 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-30 15:49 [Bug c/50581] New: stdarg doesn't support array types Wolfgang at Solfrank dot net
2011-09-30 20:13 ` [Bug c/50581] " joseph at codesourcery dot com
2011-10-01 11:07 ` Wolfgang at Solfrank dot net
2011-10-01 14:08 ` joseph at codesourcery dot com
2011-10-01 14:43 ` Wolfgang at Solfrank dot net
2011-10-01 18:46 ` joseph at codesourcery dot com
2011-10-02 16:09 ` Wolfgang at Solfrank dot net
2011-10-02 19:48 ` svfuerst at gmail dot com
2022-01-03  2:38 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).