https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81028
Hubert Tong <hstong at ca dot ibm.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Keywords| |wrong-code Version|7.0 |8.0 Summary|miscompiles placement new |GCC miscompiles placement | |new --- Comment #1 from Hubert Tong <hstong at ca dot ibm.com> --- In the following program, the only iteration of the loop increments the int object pointed-to by p, and then replaces said object via q using placement-new. GCC miscompiles at -O2, at least with x86_64-pc-linux-gnu. Online compiler: https://wandbox.org/permlink/ByNZmtd6oB6SgG0g ### SOURCE (<stdin>): inline void* operator new(decltype(sizeof 0), void* __p) noexcept { return __p; } extern "C" void abort(); typedef int A; typedef float B; B *qq; void foo(A *p, A *q, long unk) { for (long i = 0; i < unk; ++i) { ++*p; qq = new (static_cast<void *>(&q[i])) B(42); } } void (*volatile fp)(A *, A *, long); int main(void) { union { A x; B f; } u = { 0 }; fp = foo; fp(&u.x, &u.x, 1); if (*qq != 42) abort(); } ### COMMANDS TO REPRODUCE: g++ -O2 -o ./a.out -x c++ -std=c++11 - ./a.out ### EXPECTED OUTPUT: (rc=0) ### ACTUAL OUTPUT: Aborted (rc=134) ### COMPILER VERSION INFO (g++ -v): Using built-in specs. COLLECT_GCC=/opt/wandbox/gcc-head/bin/g++ COLLECT_LTO_WRAPPER=/opt/wandbox/gcc-head/libexec/gcc/x86_64-pc-linux-gnu/8.0.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: ../source/configure --prefix=/opt/wandbox/gcc-head --enable-languages=c,c++ --disable-multilib --without-ppl --without-cloog-ppl --enable-checking=release --disable-nls --enable-lto LDFLAGS=-Wl,-rpath,/opt/wandbox/gcc-head/lib,-rpath,/opt/wandbox/gcc-head/lib64,-rpath,/opt/wandbox/gcc-head/lib32 Thread model: posix gcc version 8.0.0 20170607 (experimental) (GCC)