[Bug c++/38297] New: O2 causes invalid code
I am compiling with g++ on a 64 bit Ubuntu OS. I spent days trying to find out why my programs kept crashing. I found that when using O2 optimizations the problem is there, when using O1 it is not a problem. I am not using any compiler options other than stripping symbols, hiding warnings, and O2. Here is a very simple test case that causes the bug every time. void RefTest(byte*& p) { ++p; } void RefTest(char*& p) { ++p; } char* c= "0123"; char* p= c; RefTest((byte*&)p); cout << p << endl; // 0123 RefTest(p); cout << p << endl; // 123 return 0; -- Summary: O2 causes invalid code Product: gcc Version: 4.3.2 Status: UNCONFIRMED Severity: critical Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: andrew at warnux dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38297
[Bug c++/38297] O2 causes invalid code
--- Comment #1 from andrew at warnux dot com 2008-11-28 00:32 --- I guess you want this too: gcc -v Using built-in specs. Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.3.2-1ubuntu11' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.3.2 (Ubuntu 4.3.2-1ubuntu11) -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38297
[Bug c++/38297] O2 causes invalid code
--- Comment #3 from andrew at warnux dot com 2008-11-28 01:59 --- unsigned char char and byte are the same size (1 byte), so why is there a problem? -- andrew at warnux dot com changed: What|Removed |Added Status|WAITING |UNCONFIRMED http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38297
[Bug c++/38297] O2 causes invalid code
--- Comment #5 from andrew at warnux dot com 2008-11-28 02:33 --- Thanks! I guess I learned something new today. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38297
[Bug c++/38297] O2 causes invalid code
--- Comment #6 from andrew at warnux dot com 2008-11-28 16:38 --- I do have a couple concerns: 1. This one is MAJOR. Without using -Wstrict-aliasing, I never see warnings about this change. I can't even begin to explain how bad that is. The gcc programmers made a big change (that is default compile) and there isn't even a warning by default! I wonder how many other things like this have been done. 2. Will other compilers compile this kind of code correctly? inline bool IsNegative(float f) { union { float f; int i; } u; u.f = f; return u.i & 0x8000; } http://xania.org/200712/cpp-strict-aliasing -- andrew at warnux dot com changed: What|Removed |Added Status|RESOLVED|UNCONFIRMED Resolution|DUPLICATE | http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38297
[Bug c++/38297] O2 causes invalid code
--- Comment #9 from andrew at warnux dot com 2008-11-28 22:01 --- I have another question. I want to be able to detect if fno-strict-aliasing was used when compiling. Preferably at compile time but run time will be fine. How can I do this? -- andrew at warnux dot com changed: What|Removed |Added Status|RESOLVED|UNCONFIRMED Resolution|DUPLICATE | http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38297
[Bug c++/38297] O2 causes invalid code
--- Comment #10 from andrew at warnux dot com 2008-11-28 22:02 --- The shortest answer possible will be fine. I don't want to be an annoyance. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38297