public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "Wolfgang at Solfrank dot net" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/50581] New: stdarg doesn't support array types
Date: Fri, 30 Sep 2011 15:49:00 -0000	[thread overview]
Message-ID: <bug-50581-4@http.gcc.gnu.org/bugzilla/> (raw)

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


             reply	other threads:[~2011-09-30 15:23 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-30 15:49 Wolfgang at Solfrank dot net [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-50581-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).