dynamic_cast<>() segfaults !

Max Lawson mlawson@drfmc.ceng.cea.fr
Sat Feb 14 09:32:00 GMT 1998


/*
  >> Hi, 

  >> The following testcase demonstrate a bug in the dynamic_cast 
  >> implementation. 
  
  >> First, here's gdb output:

  $ g++ -g -Wall bug.cc
  $ a.out 
  Segmentation fault (core dumped)
  $ gdb a.out core 
  GDB is free software and you are welcome to distribute copies of it
  under certain conditions; type "show copying" to see the conditions.
  There is absolutely no warranty for GDB; type "show warranty" for details.
  GDB 4.16 (i586-unknown-linux), Copyright 1996 Free Software Foundation, Inc..
  Core was generated by `a.out'.
  Program terminated with signal 11, Segmentation fault.
  Reading symbols from /usr/local/lib/libstdc++.so.2.8...done.
  Reading symbols from /lib/libm.so.5...done.
  Reading symbols from /lib/libc.so.5...done.
  Reading symbols from /lib/ld-linux.so.1...done.
  #0  0x8048800 in test_ptr (ctxt=0x8049f58) at bug.cc:29
  29        S1* ctxt2 = dynamic_cast<S1*>(ctxt1);
  (gdb) 

  >> I'm running "egcs-2.91.07 980205 (gcc-2.8.0 release)" compiled for 
  >> a i586-pc-linux-glibc1 box

  >> Note that there is a "NO_FAILURE" flag to show that 
  >> the failure occurs only when using multiple inheritance. 
  
  >> I don't know if the code is legal ? IMHO, it is. Anyway, if someone 
  >> could let me know 

  >> Thanx in advance 

  >> P.S. Could you add this to the g++testsuite.. my 10^-99$ contrib.

 */


/* --------bug.cc--------- */

#include "iostream.h"


class S0
{
public:

  S0() { };

  virtual ~S0() { }
};



#ifndef NO_FAILURE 
struct S { };
class S1 : public S, public S0
#else 
class S1 : public S0
#endif
{ 
public:

  S1() { }
};


void test_ptr(void *ctxt)
{
  S0 *ctxt1 = static_cast<S0*>(ctxt);

  S1* ctxt2 = dynamic_cast<S1*>(ctxt1);

  cout << "ctxt: " << ctxt2 << endl; 
}


int main()
{
  S1 *ctxt = new S1();

  test_ptr(ctxt);

  return 0;
}



More information about the Gcc mailing list