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

            Bug ID: 70644
           Summary: Warn about implicit conversion of 'this' to pointer to
                    virtual base class during construction
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

(Reduced from PR 58822)

struct Base { Base(int) { } };

int foo(Base*) { return 0; }

struct X : virtual Base {
  X() : Base(foo(this)) { }
};

int main() {
  X x;
}

The implicit conversion in the call foo(this) is undefined behaviour. It
violates [basic.life] 3.8p6 (6.3) by converting the object's address to a
pointer to virtual base before it is constructed.

There is no warning, and no ubsan error.

If the implicit conversion happens in a different scope, not inside the
constructor, then we get a ubsan error (and segfault):

struct Base { Base(int) { } };

struct X;
int foo(X*);

struct X : virtual Base {
  X() : Base(foo(this)) { }
};

int foo(X* x) { Base* b = x; return 0; }

int main() {
  X x;
}

vb.cc:10:27: runtime error: cast to virtual base of address 0x7ffd25ef32f0
which does not point to an object of type 'X'
0x7ffd25ef32f0: note: object has invalid vptr
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  90 0a 40 00 00
00 00 00  80 65 20 63
              ^~~~~~~~~~~~~~~~~~~~~~~
              invalid vptr
Segmentation fault (core dumped)


Since the original example is also UB it would be good to either get a
diagnostic from the front end at the point of the implicit conversion, or at
least get a ubsan error..

Reply via email to