This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Request for a C++ warning for undefined behaviour


I spent a good amount of today while debugging some C++ program which
conceptually was doing something similar to the below code, which
segfaults, when run:
------ snip -----
struct A {
        A(A*) {}
        virtual void g() {}
struct B : public A {
        virtual A* f(A*);
        B() : A(f(this)) {}

A* B::f(A *x) { return dynamic_cast<B*>(x); }

int main()
  B b;
  return 0;
------ snap -----

Well, if one manages to cut the thing down to this size the problem
becomes easy to see, but if I had have a warning my day would have been
saved ;-)  Basically I'm asking for a warning, or even an error when a
member function is called to form arguments for the initialization of base
classes, as per 12.6.2 #8 this is undefined behaviour.  You can call
member functions on an object under construction only, when all base
classes are initialized, and in this case it's trivially violated.

The real code in question was much larger, and had a more complicated
inheritance structure, where in fact the equivalent of B::f() above did
access the vtable of 'this' in non-obvious ways (namely for calling
other member functions) instead of explicitely passing 'this' like the
above example does.  The segfault of course also happened ;-)

I guess in the above trivial cases (i.e. calling member functions in
arguments to base-class initializers) g++ can even issue an error, as this
is already undefined by definition.  Of course this doesn't apply to cases
where a member is called through a sequence of indirections, which also is
undefined, but in my case the above would have been enough.

Is that feasible?


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]