[gnu.g++.bug] virtual function bug...

andersnb@CS.NYU.EDU (Brian G. Anderson) (02/04/90)

I just got bitten by a bug in virtual functions with virtual base classes.
The code follow along with the segmentation error reported when run under
gdb.  The code should print out:
	four
	two
	hello]one
	two
but as one can see when examining the run output it gets to "hello]one"
before jumping to a non-existent location.  The program seems correct to
me.  I'm dead in the water until I can figure out how to correct this.
Am I doing something wrong?  If I am why, isn't the compiler telling me?
If I am not, is there a patch to fix this problem?  Help!

-Brian G. Anderson		New York University
andersnb@cs.nyu.edu

--------the code----------
#include <stream.h>

class bottom {
public:
  char* hold;
  bottom(char* txt) { hold = txt; }
  virtual void one() const = 0;
};

class leftone: public virtual bottom {
public:
  virtual void two() const = 0;
  void one() const { cout << hold << "] one\n"; two(); }
};

class lefttwo: public leftone {
public:
  void two() const { cout << "two\n"; }
};

class right: public virtual bottom {
public:
  void three() const { cout << "three\n"; one(); }
};

class top: public right, public lefttwo {
public:
  top(char* txt): bottom(txt) {};
  void four() const { cout << "four\n"; two(); one(); }
};

main() {
  top x("hello");
  x.four();
}

-------compile and run--------
(shasha)% g++ -g -v test.cc
g++ version 1.36.4 (based on GCC 1.36.93)
 /usr/src/local/usr/local/lib/gcc-cpp -+ -v -undef -D__GNUC__ -D__GNUG__ -D__cplusplus -Dsparc -Dsun -Dunix -D__sparc__ -D__sun__ -D__unix__ test.cc /usr/tmp/cca07957.cpp
GNU CPP version 1.36.92
 /usr/src/local/usr/local/lib/gcc-cc1plus /usr/tmp/cca07957.cpp -quiet -dumpbase test.cc -g -version -o /usr/tmp/cca07957.s
GNU C++ version 1.36.4 (based on GCC 1.36.93) (sparc) compiled by GNU C version 1.36.92.
default target switches: -mfpu -mepilogue
 /usr/src/local/usr/local/lib/gcc-as -o test.o /usr/tmp/cca07957.s
 /usr/src/local/usr/local/lib/gcc-ld -e start -dc -dp -Bstatic /lib/crt0.o test.o -lg++ /usr/src/local/usr/local/lib/gcc-gnulib -lg -lc
(shasha)% gdb a.out
GDB 3.4, Copyright (C) 1989 Free Software Foundation, Inc.
There is ABSOLUTELY NO WARRANTY for GDB; type "info warranty" for details.
GDB is free software and you are welcome to distribute copies of it
 under certain conditions; type "info copying" to see the conditions.
Reading symbol data from /shasha/andersnb/plinda/a.out...done.
Type "help" for a list of commands.
(gdb) run
Starting program: a.out 
four
two
hello] one

Program received signal 11, Segmentation fault
0x0 in _DYNAMIC ()
(gdb) quit
The program is running.  Quit anyway? (y or n) y
(shasha)% exit
(shasha)% 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Brian G. Anderson                                       |
NYU Ultracomputer Research Project                     |||
715 Broadway  Rm. 1006                                |||||
New York, NY  10003                                   |||||
(212) 998-3346                                     --- //\ ---
arpa:	andersnb@cmcl2                             ----/ \----
uucp:	{ihnp4,seismo}!cmcl2!andersnb              ----   ----

tiemann@arkesden.eng.sun.com (Michael Tiemann) (02/05/90)

   Date: Sat, 3 Feb 90 17:42:41 -0500
   From: andersnb@cs.nyu.edu (Brian G. Anderson)

   I just got bitten by a bug in virtual functions with virtual base classes.
   The code follow along with the segmentation error reported when run under
   gdb.  The code should print out:
	   four
	   two
	   hello]one
	   two
   but as one can see when examining the run output it gets to "hello]one"
   before jumping to a non-existent location.  The program seems correct to
   me.  I'm dead in the water until I can figure out how to correct this.
   Am I doing something wrong?  If I am why, isn't the compiler telling me?
   If I am not, is there a patch to fix this problem?  Help!

   -Brian G. Anderson		New York University
   andersnb@cs.nyu.edu

   --------the code----------
   #include <stream.h>

   class bottom {
   public:
     char* hold;
     bottom(char* txt) { hold = txt; }
     virtual void one() const = 0;
   };

   class leftone: public virtual bottom {
   public:
     virtual void two() const = 0;
     void one() const { cout << hold << "] one\n"; two(); }
   };

   class lefttwo: public leftone {
   public:
     void two() const { cout << "two\n"; }
   };

   class right: public virtual bottom {
   public:
     void three() const { cout << "three\n"; one(); }
   };

   class top: public right, public lefttwo {
   public:
     top(char* txt): bottom(txt) {};
     void four() const { cout << "four\n"; two(); one(); }
   };

   main() {
     top x("hello");
     x.four();
   }

   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   Brian G. Anderson                                       |
   NYU Ultracomputer Research Project                     |||
   715 Broadway  Rm. 1006                                |||||
   New York, NY  10003                                   |||||
   (212) 998-3346                                     --- //\ ---
   arpa:	andersnb@cmcl2                             ----/ \----
   uucp:	{ihnp4,seismo}!cmcl2!andersnb              ----   ----


Here's a patch to try:

    arkesden% diff -c2 cplus-search.c~ cplus-search.c
    *** cplus-search.c~	Fri Jan 26 06:31:53 1990
    --- cplus-search.c	Sun Feb  4 19:53:04 1990
    ***************
    *** 2677,2680 ****
    --- 2677,2687 ----
			  }
		      ok:
    + 		    if (CLASSTYPE_OFFSET (this_context) != integer_zero_node)
    + 		      {
    + 			tree offset_entry = CONSTRUCTOR_ELTS (MI_VMATRIX (row, col)[2]);
    + 			TREE_VALUE (offset_entry)
    + 			  = genop (PLUS_EXPR, TREE_VALUE (offset_entry),
    + 				   CLASSTYPE_OFFSET (this_context));
    + 		      }
			MI_VMATRIX (row, col)[0] = 0;

    arkesden% 

Michael