This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Possible GCC bug with copy-construction of object on itself?
- From: "Ole Kniemeyer" <o_kniemeyer at maxon dot de>
- To: gcc-help at gcc dot gnu dot org
- Date: Thu, 08 Feb 2018 14:57:46 +0100
- Subject: Possible GCC bug with copy-construction of object on itself?
- Authentication-results: sourceware.org; auth=none
Hi,
I have a small code snippet where I'm not sure if it's a GCC bug or undefined behaviour, so maybe
someone can help? The code is
#include <new>
void Func(void*);
struct A
{
A() = default;
A(const A& src)
{
Func(src.x);
}
void* x;
};
void Test(void* ptr)
{
A tmp;
tmp.x = ptr;
new (&tmp) A(tmp);
}
If you paste this code at gcc.godbolt.org and use -O3, GCC <= 5.X and Clang produce the code which
I'd expect:
Test(void*):
jmp Func(void*)
But GCC 6 and 7 call Func with a nullptr:
Test(void*):
xorl %edi, %edi
jmp Func(void*)
I admit that the code is a bit dubious because it copy-constructs a new A object with itself and
the copy constructor doesn't copy anything, so is this undefined behavior or a GCC bug?
Thanks
Ole
Dr. Ole Kniemeyer
Senior Software Developer
MAXON Computer GmbH
Max-Planck-Str. 20
D-61381 Friedrichsdorf