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. Release: unknown Environment: 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 How-To-Repeat: 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"); test(ke); } --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 DOH [ root@valhalla /usr/src ] # gcc -march=i686 testcase.c \ -o testcase [ root@valhalla /usr/src ] # ./testcase ûÿ¿¨G@`a @ÈØ@8K@7û@Xýÿ¿/
Fix: 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).
From: Wolfgang Bangerth <bangerth@ticam.utexas.edu> To: gcc-gnats@gcc.gnu.org Cc: 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. W. ------------------------------------------------------------------------- Wolfgang Bangerth email: bangerth@ticam.utexas.edu www: http://www.ticam.utexas.edu/~bangerth
*** This bug has been marked as a duplicate of 5351 ***