[Patch] PR java/9685: Fix Catching Illegal Access to Package-Private Fields/Members

Ranjit Mathew rmathew@gmail.com
Tue Apr 20 13:11:00 GMT 2004


Hi,

    This patch proposes to fix PR java/9685.

Tested on i686-pc-linux-gnu by bootstrapping c,c++,java.
No testsuite regressions, except for the known
InvokeInterface failure that's unrelated to this patch.

Ok for mainline?

Notes:

1. This still does not fix catching illegal accesses to
   package-private *classes* - only fields/members. That
   needs a separate patch.

2. I chose to create a new function java_acc_ctrl_str
   instead of using java_accstring_lookup, as the latter
   does not return the desired string for package-private
   access. It also returns "bogus" strings if static, etc.
   is also flagged.

3. For not_accessible_p(), the MEMBER argument passed is
   sometimes a TYPE_DECL, that needs the extra check I
   have added. This still does not catch #1 above though. :-(

Ranjit.


Index: ChangeLog
from  Ranjit Mathew  <rmathew@hotmail.com>

	Fixes PR java/9685
	* parse.y (java_acc_ctrl_str): New method.
	(not_accessible_field_error): Use java_acc_ctrl_str()
	instead of java_accstring_lookup().
	(resolve_qualified_expression_name): Likewise.
	(patch_method_invocation): Likewise.
	(not_accessible_p): Use DECL_CONTEXT (member) instead of
	reference for package-private access checking.

Index: parse.y
===================================================================
--- parse.y	2004-03-29 21:06:35.000000000 +0530
+++ parse.y	2004-03-29 22:16:25.000000000 +0530
@@ -77,4 +77,5 @@ definitions and other extensions.  */
 /* Local function prototypes */
 static char *java_accstring_lookup (int);
+static const char *java_acc_ctrl_str (int);
 static void  classitf_redefinition_error (const char *,tree, tree, tree);
 static void  variable_redefinition_error (tree, tree, tree, int);
@@ -3183,5 +3184,5 @@ not_accessible_field_error (tree wfl, tr
   parse_error_context
     (wfl, "Can't access %s field `%s.%s' from `%s'",
-     java_accstring_lookup (get_access_flags_from_decl (decl)),
+     java_acc_ctrl_str (get_access_flags_from_decl (decl)),
      GET_TYPE_NAME (DECL_CONTEXT (decl)),
      IDENTIFIER_POINTER (DECL_NAME (decl)),
@@ -3229,4 +3230,14 @@ java_accstring_lookup (int flags)
 }

+static const char *
+java_acc_ctrl_str (int flags)
+{
+  if (flags & ACC_PRIVATE) return "private";
+  if (flags & ACC_PROTECTED) return "protected";
+  if (flags & ACC_PUBLIC) return "public";
+
+  return "package-private";
+}
+
 /* Issuing error messages upon redefinition of classes, interfaces or
    variables. */
@@ -9874,5 +9885,5 @@ resolve_qualified_expression_name (tree
 	      parse_error_context
 		(qual_wfl, "Can't access %s class '%s' from '%s'",
-		 java_accstring_lookup (get_access_flags_from_decl (decl)),
+		 java_acc_ctrl_str (get_access_flags_from_decl (decl)),
 		 IDENTIFIER_POINTER (DECL_NAME (decl)),
 		 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
@@ -10141,9 +10152,10 @@ not_accessible_p (tree reference, tree m
     }

-  /* Default access are permitted only when occurring within the
-     package in which the type (REFERENCE) is declared. In other words,
-     REFERENCE is defined in the current package */
-  if (ctxp->package)
-    return !class_in_current_package (reference);
+  /* Default access is permitted only when occurring from within the
+     package in which the context (MEMBER) is declared. */
+  if (TREE_CODE (member) == TYPE_DECL)
+    return !class_in_current_package (TREE_TYPE (member));
+  else
+    return !class_in_current_package (DECL_CONTEXT (member));

   /* Otherwise, access is granted */
@@ -10541,5 +10553,5 @@ patch_method_invocation (tree patch, tre
       const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
       const char *const access =
-	java_accstring_lookup (get_access_flags_from_decl (list));
+	java_acc_ctrl_str (get_access_flags_from_decl (list));
       const char *const klass =
 	IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));



More information about the Gcc-patches mailing list