🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.bug
Article #186

virtual function gets wrong `this'

#186
From: A.R.Runnalls
Date: Thu, 06 Aug 1992 17:08
93 lines
3188 bytes
On a Sparc running SunOS 4, using g++ 2.2.2, the following program
crashes, apparently because the `this' pointer passed to function `f' is
erroneous:

// saux_bug.cc

class R {
public:
    virtual int f() const = 0;
};

class VR : virtual public R {
public:
    virtual int g() const = 0;
    int f() const;
};

int VR::f() const
   {return g();}
   
class VR2 : public VR {
public:
    int g() const  {return 0;}
};

class L {
private:
    int foo;
};

class LVR : public L, virtual public R {
};

class LVRVR2 : public LVR, public VR2 {
};

main()
   {LVR* node = new LVRVR2;
    node->f();}

Script started on Thu Aug  6 18:01:18 1992
sauxbug% make saux_bug
rm -f saux_bug.o
g++ -c -g -v -Wall saux_bug.cc
Reading specs from /usr/local/lib/gcc-lib/sparc-sun-sunos4.1/2.2.2/specs
gcc version 2.2.2
 /usr/local/lib/gcc-lib/sparc-sun-sunos4.1/2.2.2/cpp -lang-c++ -v -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus -Dsparc -Dsun -Dunix -D__sparc__ -D__sun__ -D__unix__ -D__sparc -D__sun -D__unix -g -Wall saux_bug.cc /usr/tmp/cca26295.i
GNU CPP version 2.2.2 (sparc)
 /usr/local/lib/gcc-lib/sparc-sun-sunos4.1/2.2.2/cc1plus /usr/tmp/cca26295.i -quiet -dumpbase saux_bug.cc -g -Wall -version -o /usr/tmp/cca26295.s
GNU C++ version 2.2.2 (sparc) compiled by GNU C version 2.2.2.
 as -o saux_bug.o /usr/tmp/cca26295.s
g++ -g -v -o saux_bug saux_bug.o
Reading specs from /usr/local/lib/gcc-lib/sparc-sun-sunos4.1/2.2.2/specs
gcc version 2.2.2
 /usr/local/lib/gcc-lib/sparc-sun-sunos4.1/2.2.2/ld -e start -dc -dp -o saux_bug /lib/crt0.o -L/usr/local/lib/gcc-lib/sparc-sun-sunos4.1/2.2.2 -L/usr/local/lib saux_bug.o -lg++ -lgcc -lc -lg -lgcc
sauxbug% gdb !$
gdb saux_bug
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.5, Copyright 1992 Free Software Foundation, Inc...
(gdb) b 14
Breakpoint 1 at 0x2290: file saux_bug.cc, line 14.
(gdb) run
Starting program: /u1/users/cur/arr/bug_reports/sauxbug/saux_bug 

Breakpoint 1, VR::f (this=0x6158) at saux_bug.cc:15
15	   {return g();}
(gdb) p *this
$1 = {
<R> = <invalid address 0x0>
  members of VR: _vb$R = 0x0, _vptr$VR = 0x6168}
(gdb) c
Continuing.

Program received signal 11, Segmentation fault
0x0 in ?? ()
(gdb) bt
#0  0x0 in ?? ()
#1  0x23b4 in main () at saux_bug.cc:35
(gdb) f 1
#1  0x23b4 in main () at saux_bug.cc:35
35	    node->f();}
(gdb) p *node
$2 = {
<L> = {foo = 0}
<R> = {_vptr$FOO = 0x40d8 <LVRVR2$R virtual table>}
  members of LVR: _vb$R = 0x6168}
(gdb) q
The program is running.  Quit anyway? (y or n) y
sauxbug% exit
sauxbug% 
script done on Thu Aug  6 18:02:30 1992
Technical Details
Message-ID: <4@larch.ukc.ac.uk>
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!ukc.ac.UK!arr

Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.bug
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!ukc.ac.UK!arr
From: arr@ukc.ac.UK (A.R.Runnalls)
Subject: virtual function gets wrong `this'
Message-ID: <4@larch.ukc.ac.uk>
Sender: gnulists@ai.mit.edu
Organization: Computing Lab, University of Kent at Canterbury, UK.
Distribution: gnu
Date: Thu, 6 Aug 1992 17:08:41 GMT
Approved: bug-g++@prep.ai.mit.edu
Lines: 94