https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95638

            Bug ID: 95638
           Summary: Legit-looking code doesn't work with -O2
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: officesamurai at gmail dot com
  Target Milestone: ---

Created attachment 48718
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48718&action=edit
The failing code

The attached code produces wrong result when build with -O2 and without
-fno-strict-aliasing, but I don't believe it does anything illegal. The only
fishy thing it does is creating a pointer to T before an object of that type is
constructed at that location (in Storage::Storage and Storage::push_back), but
the pointer is just passed to the placement new, which should be fine according
to [basic.life] (because "...using the pointer as if the pointer were of type
void*, is well-defined").

------------------------
$ g++-10.1.0 -v
Using built-in specs.
COLLECT_GCC=g++-10.1.0
COLLECT_LTO_WRAPPER=/home/brd/soft/gcc-10.1.0/libexec/gcc/x86_64-pc-linux-gnu/10.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ./configure --prefix=/home/brd/soft/gcc-10.1.0
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.1.0 (GCC) 

$ g++-10.1.0 -O2 gcc10_aliasing_issue.cpp -o test -std=c++14 && ./test
Null!!!

$ g++-10.1.0 -O2 -fno-strict-aliasing gcc10_aliasing_issue.cpp -o test
-std=c++14 && ./test
OK
------------------------

P.S. The same code works fine with GCC 9 and earlier versions.

Reply via email to