http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57504

            Bug ID: 57504
           Summary: invalid this pointer passed in call to virtual
                    function that returns a struct
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bugzilla at cems dot de

/*The following code compiles without diagnostic in both gcc 4.6.2 and gcc
4.7.2.  It produces correct code in gcc 4.6.2 but incorrect code in gcc 4.7.2.
The error occurs with or without optimization.  No other compiler switches were
used.  The mingw builds of gcc are used.
*/

#include <iostream>
using namespace std;

struct S { int a, b, c, d; }; //must be more than 8 bytes to generate bug

struct Base
{ int i;
  virtual S f() = 0;
  void g() { f(); }
};

struct Middle : virtual Base  //intermediate class needed to generate bug
{ int i;
};

struct Derived : Middle
{ S f()
  { cerr << this << endl
    return S();
  }
};

int main()
{ Derived d;
  d.f();
  d.g();  //incorrect value of this pointer in gcc 4.7.2
  d.f();
}

/*
When compiled with gcc 462, all three calls to d.f() and d.g() show the same
value of the "this" pointer.

When compiled with gcc 472, the call to g.d() shows a wrong this pointer.

In code examples where the function called with an invalid this pointer
accesses members of the class, a run-time segmentation fault is typicallly
generated.

Workaround:  When using virtual fucntions that return structures, gcc 4.7.x
should not be used. gcc 4.6.x is ok.  gcc 4.8.x and later are not yet available
for mingw.
*/

Reply via email to