https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70620

            Bug ID: 70620
           Summary: possible wrong code at -Os on x86_64-linux-gnu for C++
                    code with multiple inheritance and casting
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: su at cs dot ucdavis.edu
  Target Milestone: ---

The current gcc trunk possibly miscompiles the following code on
x86_64-linux-gnu at -Os in both 32-bit and 64-bit modes.

It also affects 5.x at -Os and above and seems to be a regression from 4.9.x. 


$ g++-trunk -v
Using built-in specs.
COLLECT_GCC=g++-trunk
COLLECT_LTO_WRAPPER=/usr/local/gcc-trunk/libexec/gcc/x86_64-pc-linux-gnu/6.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-source-trunk/configure --enable-languages=c,c++,lto
--prefix=/usr/local/gcc-trunk --disable-bootstrap
Thread model: posix
gcc version 6.0.0 20160410 (experimental) [trunk revision 234869] (GCC) 
$ 
$ g++-trunk -O1 small.cpp; ./a.out; echo $?
0
$ g++-4.9 -Os small.cpp; ./a.out; echo $?
0
$ 
$ g++-trunk -Os small.cpp
$ ./a.out
Segmentation fault (core dumped)
$ 


--------------------------------------------


int a; 

class A 
{ 
public: 
  virtual ~A () {} 
};

class B1 : public A {};
class B2 : public A {};

class C 
{ 
public: 
  virtual ~C () {} 
};

class D : public C, public B2 
{ 
public: 
  virtual ~D () {} 
};

class E : public B1, virtual public D
{ 
public: 
  virtual ~E () {  a = 0; } 
};

int
main ()
{
  a = 1;
  delete (D *) (B1 *) new E;  // Does this lead to undefined behavior?  
  if (a) 
    return 1; 
  return 0; 
}

Reply via email to