This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[C++ PATCH] Fix PR11174 (pmf access checking)


Hi

This patch implements access checking for pointer to member
according to 11.5p1.  When a pointer to non-static member '&X::p'
is formed, access must be through the class 'X'.

Tested on i686-pc-linux-gnu.  OK to commit to for trunk?

--Kriang


2003-08-18  Kriang Lerdsuwanakij  <lerdsuwa@users.sourceforge.net>

	PR c++/11174
	* init.c (build_offset_ref): Perform access checking for
	pointer to member correctly.

2003-08-18  Kriang Lerdsuwanakij  <lerdsuwa@users.sourceforge.net>

	PR c++/11174
	* g++.dg/parse/access4.C: New test.
	* g++.dg/parse/access5.C: Likewise.
	* g++.old-deja/g++.jason/access17.C: Adjust error message.


diff -cprN gcc-main-save/gcc/cp/init.c gcc-main-new/gcc/cp/init.c
*** gcc-main-save/gcc/cp/init.c	Tue Aug 12 19:48:47 2003
--- gcc-main-new/gcc/cp/init.c	Sat Aug 16 21:48:28 2003
*************** build_offset_ref (tree type, tree name, 
*** 1504,1511 ****
  	  /* Get rid of a potential OVERLOAD around it */
  	  t = OVL_CURRENT (t);
  
! 	  /* unique functions are handled easily.  */
! 	  perform_or_defer_access_check (basebinfo, t);
  	  mark_used (t);
  	  if (DECL_STATIC_FUNCTION_P (t))
  	    return t;
--- 1504,1523 ----
  	  /* Get rid of a potential OVERLOAD around it */
  	  t = OVL_CURRENT (t);
  
! 	  /* Unique functions are handled easily.  */
! 
! 	  /* For non-static member of base class, we need a special rule
! 	     for access checking [class.protected]:
! 
! 	       If the access is to form a pointer to member, the
! 	       nested-name-specifier shall name the derived class
! 	       (or any class derived from that class).  */
! 	  if (address_p && DECL_P (t)
! 	      && DECL_NONSTATIC_MEMBER_P (t))
! 	    perform_or_defer_access_check (TYPE_BINFO (type), t);
! 	  else
! 	    perform_or_defer_access_check (basebinfo, t);
! 
  	  mark_used (t);
  	  if (DECL_STATIC_FUNCTION_P (t))
  	    return t;
*************** build_offset_ref (tree type, tree name, 
*** 1517,1522 ****
--- 1529,1539 ----
  	  member = fnfields;
  	}
      }
+   else if (address_p && TREE_CODE (member) == FIELD_DECL)
+     /* We need additional test besides the one in
+        check_accessibility_of_qualified_id in case it is
+        a pointer to non-static member.  */
+     perform_or_defer_access_check (TYPE_BINFO (type), member);
  
    if (!address_p)
      {
diff -cprN gcc-main-save/gcc/testsuite/g++.dg/parse/access4.C gcc-main-new/gcc/testsuite/g++.dg/parse/access4.C
*** gcc-main-save/gcc/testsuite/g++.dg/parse/access4.C	Thu Jan  1 07:00:00 1970
--- gcc-main-new/gcc/testsuite/g++.dg/parse/access4.C	Sat Aug 16 21:02:00 2003
***************
*** 0 ****
--- 1,20 ----
+ // { dg-do compile }
+ 
+ // Origin: Mike Reed <mike.reed@amadron.com>
+ 
+ // PR c++/11174: Access checking of pointer-to-member function
+ 
+ class A {
+ protected:
+   void foo() {}			// { dg-error "protected" }
+ public:
+   A();
+ };
+ 
+ class B : public A {
+   void bar() {
+     A a;
+     void (A::*pmf)() = &A::foo;	// { dg-error "this context" }
+     (a.*pmf)();
+   }
+ };
diff -cprN gcc-main-save/gcc/testsuite/g++.dg/parse/access5.C gcc-main-new/gcc/testsuite/g++.dg/parse/access5.C
*** gcc-main-save/gcc/testsuite/g++.dg/parse/access5.C	Thu Jan  1 07:00:00 1970
--- gcc-main-new/gcc/testsuite/g++.dg/parse/access5.C	Sat Aug 16 23:16:21 2003
***************
*** 0 ****
--- 1,18 ----
+ // { dg-do compile }
+ 
+ // Origin: Giovanni Bajo <giovannibajo@libero.it>
+ 
+ // PR c++/11174: Access checking on pointer to member data.
+ 
+ struct A 
+ {
+ protected:
+   int a;			// { dg-error "protected" }
+ };
+ 
+ struct B : A 
+ {
+   void foo() {
+     (void)&A::a;		// { dg-error "this context" }
+   }
+ };
diff -cprN gcc-main-save/gcc/testsuite/g++.old-deja/g++.jason/access17.C gcc-main-new/gcc/testsuite/g++.old-deja/g++.jason/access17.C
*** gcc-main-save/gcc/testsuite/g++.old-deja/g++.jason/access17.C	Thu May  1 18:34:59 2003
--- gcc-main-new/gcc/testsuite/g++.old-deja/g++.jason/access17.C	Sat Aug 16 23:18:45 2003
*************** struct B: public A {
*** 13,19 ****
  };
  
  int A::* B::p = &A::i;         // { dg-error "" } 
! int (A::* B::fp)() = &A::f;    // ERROR -
  
  struct C {
    static int A::*p;
--- 13,19 ----
  };
  
  int A::* B::p = &A::i;         // { dg-error "" } 
! int (A::* B::fp)() = &A::f;    // { dg-error "" }
  
  struct C {
    static int A::*p;


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]