egcs bug?

Joyce Janczyn janczyn@cygnus.com
Wed Jan 20 08:41:00 GMT 1999


I'm not positive this is an egcs bug since this is pretty wicked code.
It does work correctly on other compilers though.

The problem seem to be with calling a member function via a pointer
when the function is a virtual and the class its in has multiple
inheritence. 

	(object.*fooFunc)();

If you look at the address of object prior to the call and then
at the value of this in Tobject::foo() you will see the adress of
object has been decremented by 4 in foo().  This results in bogus
data when you call valid() which should return 0x12345678.

It appears egcs is inserting a __thunk_4__ in the call to bump the
pointer to allow for a virtual pointer.   It appears this isn't the
right thing to do when the method is being called via a function pointer.


This is on Intel Linux 2.0.34 with the egcs.1.1.1.

Compile with:

	c++ -g -o bug  bug.cpp

If the error occurs you will see:

	!!!!!!!!!!!!! invalid object .....

#include <stream.h>

class Tbase
{
public:
	virtual ~Tbase();

	// If this method is virtual then the object passed
	// through the member function pointer is invalid
	// Its ben thunked when it shouldn't be.
	//
	virtual void	foo () const;
};

class Tbase2
{
public:
	virtual		~Tbase2();
};

class Tobject : public Tbase2, public Tbase {
public:
			Tobject() { fValid = 0x12345678; };
	virtual		~Tobject();

	virtual void	foo () const;

	int		valid() const { return fValid; };

private:
	int		fValid;
};

class Tfoo
{
public:
    typedef void (Tobject::*TfooFunc)() const;
};


main()
{
	Tfoo::TfooFunc fooFunc;
	fooFunc = (Tfoo::TfooFunc)&Tobject::foo;

	Tobject object;
	(object.*fooFunc)();
}


void
Tobject::foo () const
{
	if(valid() != 0x12345678) {
		cerr << "!!!!!!!!!!!!! invalid object " << valid() << "\n";
	}else{
		cerr << "OK\n";
	}
}

void
Tbase::foo () const
{ }

Tbase::~Tbase()
{ }

Tobject::~Tobject()
{ }

Tbase2::~Tbase2()
{ }


-- 
Joyce Janczyn
Cygnus Solutions
Vox: +1 416 482 4578
Fax: +1 416 482 6299



More information about the Gcc-bugs mailing list