User account creation filtered due to spam.

Bug 14688 - Mis-matched calling convention on virtual functions accepted without error
Summary: Mis-matched calling convention on virtual functions accepted without error
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.0
: P2 normal
Target Milestone: 4.3.0
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
: 29909 (view as bug list)
Depends on:
Blocks:
 
Reported: 2004-03-23 11:11 UTC by Danny Smith
Modified: 2007-09-25 00:31 UTC (History)
2 users (show)

See Also:
Host: i386-pc-mingw32
Target: i386-pc-mingw32
Build: i386-pc-mingw32
Known to work:
Known to fail: 2.95.3 3.2.3 3.3.4 3.4.0 4.0.0
Last reconfirmed: 2005-06-20 04:11:30


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Danny Smith 2004-03-23 11:11:04 UTC
Hello,
The following i386 bug was reported to mingw list by Justin Forest.

No error nor even a warning message is emitted when a virtual function in a
derived class uses a different calling convention than the function
in the base class. In the testcase that follows, this leads to the passing
of undefined parameters to the inherited method.

// virtual_mismatch.cc
 
#include <stdio.h>

class one {
public:
	virtual void test(void* value);
};

class two : public one {
public:
	void  __attribute__((regparm(2)))  test(void* value);
};


void one::test(void* value)
{
  printf("one::test(%p, %p)\n", this, value);
}

void two::test(void* value)
{
  printf("two::test(%p, %p)\n", this, value);
}


int main(int argc, const char **argv)
{
	two t;
	one *o = &t;
	t.test(o);  // a direct call,
		    // parameters passed through registers, ok
	o->test(o); // called through one::vtable,
 		    // parameters put to stack, read from registers
	return 0;
}


This compiles without warning;

> g++ -Wall -ovirtual_mismatch virtual_mismatch,cc

and produces:
 
> virtual_mismatch
> two::test(0022FF68, 0022FF68)
> two::test(00401282, 004041CC)

The problem was reported against 3.3.3 but occurs also on trunk and 3.4.

Danny
Comment 1 Andrew Pinski 2004-03-23 14:46:53 UTC
Confirmed.
Comment 2 Massimiliano Ghilardi 2006-11-20 12:21:47 UTC
*** Bug 29909 has been marked as a duplicate of this bug. ***
Comment 3 Massimiliano Ghilardi 2006-11-20 14:04:44 UTC
Duplicated Bug 29909 also points out the not-so-obvious case where the overridden virtual method is actually the automatically-compiler-generated derived class destructor:

// compile with g++ -Wall -O

class one {
public:
	virtual __attribute((regparm(3))) ~one();
};

class two : public one {
};

int main() { 
	one * o = new two();                                                                                                                          	delete o;                                                                                                                                     	return 0;                                                                                                                                 }
Comment 4 Justin Forest 2007-05-14 10:57:48 UTC
$ g++ --version
i686-apple-darwin8-g++- 4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build
5250)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

$ g++ -o test test.cc && ./test
two::test(0xbffff7d8, 0xbffff7d8)
two::test(0xbffff7d8, 0x1b94)

Still looks like a valid issue.
Comment 5 Danny Smith 2007-09-19 00:58:55 UTC
Patch at:
http://gcc.gnu.org/ml/gcc-patches/2007-09/msg01498.html

Danny
Comment 6 patchapp@dberlin.org 2007-09-19 02:24:05 UTC
Subject: Bug number PR 14688

A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2007-09/msg01498.html
Comment 7 dannysmith@gcc.gnu.org 2007-09-25 00:29:52 UTC
Subject: Bug 14688

Author: dannysmith
Date: Tue Sep 25 00:29:42 2007
New Revision: 128740

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=128740
Log:
	PR c++/14688
	* config/i386/i386.c (ix86_comp_type_attributes): Check
	METHOD_TYPE too.

cp
	* search.c (check_final_overrider): Fail if
	targetm.comp_type_attributes returns 0.

testsuite
	* g++.dg/inherit/override_attribs.C: New file.

Added:
    trunk/gcc/testsuite/g++.dg/inherit/override-attribs.C
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/config/i386/i386.c
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/search.c
    trunk/gcc/testsuite/ChangeLog

Comment 8 Danny Smith 2007-09-25 00:31:14 UTC
Fixed on trunk