This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Re: [Patch] PR java/9685: Fix Catching Illegal Access to Package-Private Fields/Members
Andrew Haley wrote:
> The fact that we allowed this corner case isn't a reason to reject
> your patch. We allowed this access, but for the wrong reasons.
Thank you.
I am attaching a slightly revised version of my patch
incorporating Bryce's suggestions. I have chosen the name
"accessibility_string" instead of "member_accessibility_string"
for the new method, as this can also be used in the
case of package-private classes/interfaces.
I also hope the comment for the function properly
explains why it is needed.
Ok for mainline?
Thanks,
Ranjit.
Index: ChangeLog
from Ranjit Mathew <rmathew@hotmail.com>
Fixes PR java/9685
* parse.y (accessibility_string): New method.
(not_accessible_field_error): Use accessibility_string()
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-04-22 10:23:32.000000000 +0530
+++ parse.y 2004-04-22 19:01:00.000000000 +0530
@@ -76,6 +76,7 @@ definitions and other extensions. */
/* Local function prototypes */
static char *java_accstring_lookup (int);
+static const char *accessibility_string (int);
static void classitf_redefinition_error (const char *,tree, tree, tree);
static void variable_redefinition_error (tree, tree, tree, int);
static tree create_class (int, tree, tree, tree);
@@ -3182,7 +3183,7 @@ 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)),
+ accessibility_string (get_access_flags_from_decl (decl)),
GET_TYPE_NAME (DECL_CONTEXT (decl)),
IDENTIFIER_POINTER (DECL_NAME (decl)),
IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
@@ -3228,6 +3229,21 @@ java_accstring_lookup (int flags)
#undef COPY_RETURN
}
+/* Returns a string denoting the accessibility of a class or a member.
+ We need a separate function from java_accstring_lookup, as the latter
+ can return spurious "static", etc. if package-private access is
+ defined (in which case none of the relevant bits are set). */
+
+static const char *
+accessibility_string (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. */
@@ -9873,7 +9889,7 @@ 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)),
+ accessibility_string (get_access_flags_from_decl (decl)),
IDENTIFIER_POINTER (DECL_NAME (decl)),
IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
return 1;
@@ -10140,11 +10156,12 @@ not_accessible_p (tree reference, tree m
return 1;
}
- /* 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 */
return 0;
@@ -10540,7 +10557,7 @@ 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));
+ accessibility_string (get_access_flags_from_decl (list));
const char *const klass =
IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
const char *const refklass =