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

--- Comment #2 from Eric Gallager <egallager at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #0)
> (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..

idea for a name for the proposed new warning?

Reply via email to