https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61410
Bug ID: 61410 Summary: strcat overwrites destination string when compiling with optimizations Product: gcc Version: 4.7.3 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: fox at ucw dot cz The following code #include <stdio.h> #include <string.h> int main(void) { char str[250]; strcpy(str, " "); str[0] = '_'; strcat(str, ","); puts(str); return 0; } prints only ",\n" when compiled with gcc-4.7 -O[23]. It should print "_,\n" and it does so with gcc-4.7 -O[01], gcc-4.4 -O[0123] and gcc-4.8 -O[0123]. The result of gcc-4.7 -v is Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/i486-linux-gnu/4.7/lto-wrapper Target: i486-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.3-1ubuntu1~10.04.2' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i586 --with-multilib-list=m32,m64 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu Thread model: posix gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-1ubuntu1~10.04.2) The disassembly of the code when using gcc-4.7 -O2: 080483a0 <main>: 80483a0: 55 push %ebp 80483a1: 89 e5 mov %esp,%ebp 80483a3: 83 e4 f0 and $0xfffffff0,%esp 80483a6: 81 ec 10 01 00 00 sub $0x110,%esp 80483ac: 65 a1 14 00 00 00 mov %gs:0x14,%eax 80483b2: 89 84 24 0c 01 00 00 mov %eax,0x10c(%esp) 80483b9: 31 c0 xor %eax,%eax 80483bb: 8d 44 24 12 lea 0x12(%esp),%eax 80483bf: 89 04 24 mov %eax,(%esp) 80483c2: 66 c7 44 24 12 2c 00 movw $0x2c,0x12(%esp) 80483c9: e8 a2 ff ff ff call 8048370 <puts@plt> 80483ce: 31 c0 xor %eax,%eax 80483d0: 8b 94 24 0c 01 00 00 mov 0x10c(%esp),%edx 80483d7: 65 33 15 14 00 00 00 xor %gs:0x14,%edx 80483de: 75 02 jne 80483e2 <main+0x42> 80483e0: c9 leave 80483e1: c3 ret 80483e2: e8 79 ff ff ff call 8048360 <__stack_chk_fail@plt> 80483e7: 90 nop Cheers, Milan Straka