Bug 7591 - function-related struct copy bug
Summary: function-related struct copy bug
Status: RESOLVED DUPLICATE of bug 5351
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Joe Buck
Depends on:
Reported: 2002-08-13 15:56 UTC by kelledin
Modified: 2003-06-12 00:15 UTC (History)
3 users (show)

See Also:
Host: i386-incept-linux-gnu
Target: i386-incept-linux-gnu
Build: i386-incept-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:

testcase.i.gz (3.72 KB, application/x-gzip )
2003-05-21 15:17 UTC, kelledin

Note You need to log in before you can comment on or make changes to this bug.
Description kelledin 2002-08-13 15:56:00 UTC
Ok, I'm now posting this as an official bug.

gcc 3.1 and higher (including current CVS HEAD aka gcc-3.3) have a problem passing structs (not struct pointers) to functions when those structs contain static char arrays, and -march=i686 or higher is used (-march=athlon-{xp,mp} seems to be exempt for some reason, though -march=athlon is affected).  Basically, the struct that the function receives has garbage contents.

Structs with static arrays of other types (such as int or float) do not seem to trigger this bug.


System: Linux valhalla 2.4.18-1 #4 Mon Jun 10 22:08:58 CDT 2002 i686 AuthenticAMD
Architecture: i686

        <machine, os, target, libraries (multiple lines)>
host: i386-incept-linux-gnu
build: i386-incept-linux-gnu
target: i386-incept-linux-gnu
configured with: ../gcc-3.1.1/configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --enable-version-specific-runtime-libs --enable-threads=posix --enable-languages=c,c++ --enable-shared --with-system-zlib --with-__cxa_atexit --host=i386-incept-linux-gnu --target=i386-incept-linux-gnu --build=i386-incept-linux-gnu

Below is a small testcase (same .c file that was used to generate the attached .i file).

--Begin testcase.c--
#include <stdio.h>

struct abba {
    char str[128];

void test(struct abba ke)
    printf("%s\n", ke.str);

int main()
    struct abba ke;

    strcpy(ke.str, "DOH");
--End testcase.c--

[ root@valhalla /usr/src ] # gcc -O2 -march=i686 \
testcase.c -o testcase
[ root@valhalla /usr/src ] # ./testcase
ج@`ûÿ¿¨G@`a    @ÈØ@8K@7û@Xýÿ¿/
[ root@valhalla /usr/src ] # gcc -O2 -march=i486 \
testcase.c -o testcase
[ root@valhalla /usr/src ] # ./testcase
[ root@valhalla /usr/src ] # gcc -march=i686 testcase.c \
-o testcase
[ root@valhalla /usr/src ] # ./testcase
ûÿ¿¨G@`a        @ÈØ@8K@7û@Xýÿ¿/
Comment 1 kelledin 2002-08-13 15:56:00 UTC
I know of no real fix for this problem.  It has been suggested that -maccumulate-outgoing-args triggers the problem.  Code which wishes to step around the bug should not pass structs in function headers anyways, but rather use const struct pointers, and make a copy of the struct inside the function if modifications are necessary.  Most code does this already as a practice of good coding style, but there are some notable exceptions (primarily loadkeys in kbd-1.06).
Comment 2 Wolfgang Bangerth 2002-11-06 12:59:04 UTC
From: Wolfgang Bangerth <bangerth@ticam.utexas.edu>
To: gcc-gnats@gcc.gnu.org
Subject: Re: c/7591: function-related struct copy bug
Date: Wed, 6 Nov 2002 12:59:04 -0600 (CST)

 This is probably a duplicate of 5351.
 Wolfgang Bangerth              email:           bangerth@ticam.utexas.edu
                                www: http://www.ticam.utexas.edu/~bangerth
Comment 3 Joe Buck 2002-11-08 15:17:42 UTC
*** This bug has been marked as a duplicate of 5351 ***