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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux