From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5788 invoked by alias); 30 Sep 2011 15:23:19 -0000 Received: (qmail 5779 invoked by uid 22791); 30 Sep 2011 15:23:18 -0000 X-SWARE-Spam-Status: No, hits=-2.9 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 30 Sep 2011 15:22:57 +0000 From: "Wolfgang at Solfrank dot net" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: Wolfgang at Solfrank dot net X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2011-09/txt/msg02407.txt.bz2 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; -------------------------------------------