This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Possible GCC bug with copy-construction of object on itself?


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	


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]