Bug 11174 - derived class can access protected base class member function through pointer to member function
Summary: derived class can access protected base class member function through pointer...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.3
: P2 normal
Target Milestone: 3.4.0
Assignee: Giovanni Bajo
URL:
Keywords: accepts-invalid, documentation, monitored
: 12119 14894 15308 15468 (view as bug list)
Depends on:
Blocks:
 
Reported: 2003-06-12 12:59 UTC by Mike Reed
Modified: 2004-05-25 14:38 UTC (History)
7 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2003-08-18 04:44:20


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mike Reed 2003-06-12 12:59:56 UTC
Reading specs from /usr/gcc3local/lib/gcc-lib/i686-pc-linux-gnu/3.3/specs
Configured with: ../gcc-3.3/configure --prefix=/usr/gcc3local
--enable-languages=c,c++
Thread model: posix
gcc version 3.3




With the following code:


class A {
protected:
    void foo() {}
public:
    A();
};


class B : public A {
    void bar() {
        A a;
        a.foo();
        void (A::*pmf)() = &A::foo;  // 1
        (a.*pmf)();                  // 2
    }
};


Compilation gives:
g++ -W -Wall -ansi -pedantic -c pmf.cpp
pmf.cpp: In member function `void B::bar()':
pmf.cpp:3: error: `void A::foo()' is protected
pmf.cpp:12: error: within this context

Compilation exited abnormally with code 1 at Thu Jun 12 10:28:26


But note that I am able to call the protected member function of the external
object via a pointer to member function (PMF).

Looking at the C++ spec (11.5 example see void D2::mem...) I think the line
labelled 1 should be ill-formed but since the compiler doesn't think so I am
able to gain access to the protected function on the line labelled 2.
Comment 1 Giovanni Bajo 2003-06-12 15:36:40 UTC
Confirmed. Reduced snippet:

---------------------------------
struct A 
{
protected:
    int a;
};

struct B : A 
{
    void foo() 
    {
        (void)&A::a;
    }
};
---------------------------------

This is accepted by any version of GCC but it is ill-formed (see the example in 
ยง11.5).
Comment 2 Kriang Lerdsuwanakij 2003-08-18 14:03:47 UTC
Patch submitted:

  http://gcc.gnu.org/ml/gcc-patches/2003-08/msg00998.html
Comment 3 GCC Commits 2003-08-19 12:55:05 UTC
Subject: Bug 11174

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	lerdsuwa@gcc.gnu.org	2003-08-19 12:55:00

Modified files:
	gcc/cp         : ChangeLog init.c 
	gcc/testsuite  : ChangeLog 
	gcc/testsuite/g++.old-deja/g++.jason: access17.C 
Added files:
	gcc/testsuite/g++.dg/parse: access4.C access5.C 

Log message:
	PR c++/11174
	* init.c (build_offset_ref): Perform access checking for
	pointer to member correctly.
	
	* g++.dg/parse/access4.C: New test.
	* g++.dg/parse/access5.C: Likewise.
	* g++.old-deja/g++.jason/access17.C: Adjust error message.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.3608&r2=1.3609
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/init.c.diff?cvsroot=gcc&r1=1.339&r2=1.340
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.2976&r2=1.2977
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/parse/access4.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/parse/access5.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.old-deja/g++.jason/access17.C.diff?cvsroot=gcc&r1=1.5&r2=1.6

Comment 4 Kriang Lerdsuwanakij 2003-08-19 12:55:52 UTC
Fixed in the main trunk.
Comment 5 Andrew Pinski 2003-09-01 03:54:07 UTC
*** Bug 12119 has been marked as a duplicate of this bug. ***
Comment 6 Andrew Pinski 2004-04-08 18:43:07 UTC
*** Bug 14894 has been marked as a duplicate of this bug. ***
Comment 7 Andrew Pinski 2004-05-06 12:36:20 UTC
*** Bug 15308 has been marked as a duplicate of this bug. ***
Comment 8 Andrew Pinski 2004-05-15 18:25:37 UTC
*** Bug 15468 has been marked as a duplicate of this bug. ***
Comment 9 Andrew Pinski 2004-05-17 19:17:37 UTC
Reopening for documentation.
Comment 10 Giovanni Bajo 2004-05-17 19:30:14 UTC
I'll take care of writing up changes.html/bugs.html for this. It affects more 
than pointer-to-members, and appears to be a FAQ.
Comment 11 Andrew Pinski 2004-05-25 14:38:24 UTC
Closing this one as PR 14949 is the one for the documenation.