Open hubert-reinterpretcast opened 8 years ago
This appears to still be an issue https://godbolt.org/z/73aoY656E
The shorted code which runs as expected with gcc is
// compile with -O1 -std=c++11
#include <new>
extern "C" void abort();
float foo(int *p, int *q, long unk) {
float *qq;
for (long i = 0; i < unk; ++i) {
++*p;
qq = new (static_cast<void *>(&q[i])) float(42);
}
return *qq;
}
int main(void) {
int x = 0;
float q = foo(&x, &x, 1);
if (q != 42) abort();
}
One thing that might be relevant is that when I tried to see if it was just undefined behavior by running with -fsanitize=undefined
the code just behaved properly
Extended Description
In the following program, the only iteration of the loop increments the
int
object pointed-to byp
, and then replaces said object via q using placement-new.Clang miscompiles at
-O1
at least onx86_64-unknown-linux-gnu
andpowerpc64le-unknown-linux-gnu
.Observation: passes with
-fno-strict-aliasing
.Observation:
-mllvm -print-before-all
is indistinguishable between the placement-new and the commented-outreinterpret_cast
.Online compiler: http://melpon.org/wandbox/permlink/h0aEUwT90gAnbGtv
SOURCE (
<stdin>
):COMMANDS TO REPRODUCE:
EXPECTED OUTPUT:
ACTUAL OUTPUT:
COMPILER VERSION INFO (
clang -v
):