Index: modules/luni/src/main/java/java/lang/SecurityManager.java =================================================================== --- modules/luni/src/main/java/java/lang/SecurityManager.java (revision 479590) +++ modules/luni/src/main/java/java/lang/SecurityManager.java (working copy) @@ -29,7 +29,6 @@ import java.security.AccessController; import java.security.AllPermission; import java.security.Permission; -import java.security.PrivilegedAction; import java.security.Security; import java.security.SecurityPermission; import java.util.PropertyPermission; @@ -43,7 +42,8 @@ */ public class SecurityManager { - static String[] securePackageList; + private static final String PKG_ACC_KEY = "package.access"; + private static final String PKG_DEF_KEY = "package.definition"; private static final PropertyPermission READ_WRITE_ALL_PROPERTIES_PERMISSION = new PropertyPermission( "*", "read,write"); @@ -299,30 +299,13 @@ * the name of the package to be accessed. */ public void checkPackageAccess(String packageName) { - if (packageName == null) { - throw new NullPointerException(); - } - if (securePackageList == null) { - String securePackages = getSecurityProperty("package.access"); - if (securePackages != null) { - StringTokenizer tokenizer = new StringTokenizer(securePackages, - ", "); - int i = 0; - securePackageList = new String[tokenizer.countTokens()]; - while (tokenizer.hasMoreTokens()) { - securePackageList[i++] = tokenizer.nextToken(); - } - } else { - securePackageList = new String[0]; - } - } - for (int i = 0; i < securePackageList.length; i++) { - if (packageName.startsWith(securePackageList[i])) { - checkPermission(new RuntimePermission("accessClassInPackage." - + packageName)); - return; - } - } + if (packageName == null) { + throw new NullPointerException(); + } + if (checkPackageProperty(PKG_ACC_KEY, packageName)) { + checkPermission(new RuntimePermission("accessClassInPackage." + + packageName)); + } } /** @@ -333,27 +316,42 @@ * the name of the package to add a class to. */ public void checkPackageDefinition(String packageName) { - if (packageName == null) { - throw new NullPointerException(); - } - String securePackages = getSecurityProperty("package.definition"); - if (securePackages != null) { - StringTokenizer tokenizer = new StringTokenizer(securePackages, - ", "); - while (tokenizer.hasMoreTokens()) { - if (packageName.startsWith(tokenizer.nextToken())) { - checkPermission(new RuntimePermission( - "defineClassInPackage." + packageName)); - return; - } - } - } + if (packageName == null) { + throw new NullPointerException(); + } + if (checkPackageProperty(PKG_DEF_KEY, packageName)) { + checkPermission(new RuntimePermission("defineClassInPackage." + + packageName)); + } } - private String getSecurityProperty(final String property) { - PrivilegedAction pa = PriviAction.getSecurityProperty(property); - return AccessController.doPrivileged(pa); - } + /** + * Returns true if the package name is restricted by + * the specified security property. + */ + private static boolean checkPackageProperty(String property, String pkg) { + String list = AccessController.doPrivileged( + PriviAction.getSecurityProperty(property)); + if (list != null) { + int plen = pkg.length(); + StringTokenizer tokenizer = new StringTokenizer(list, ", "); + while (tokenizer.hasMoreTokens()) { + String token = tokenizer.nextToken(); + int tlen = token.length(); + if (plen > tlen) { + if (pkg.startsWith(token) && + (token.charAt(tlen - 1) == '.' || pkg.charAt(tlen) == '.')) { + return true; + } + } + else if (plen + 1 >= tlen && token.startsWith(pkg)) { + return plen == tlen || token.charAt(tlen -1 ) == '.'; + } + } + } + + return false; + } /** * Checks whether the running program is allowed to access the system Index: modules/luni/src/main/java/org/apache/harmony/luni/util/PriviAction.java =================================================================== --- modules/luni/src/main/java/org/apache/harmony/luni/util/PriviAction.java (revision 479590) +++ modules/luni/src/main/java/org/apache/harmony/luni/util/PriviAction.java (working copy) @@ -53,8 +53,8 @@ * * @see Security#getProperty */ - public static PrivilegedAction getSecurityProperty(String property) { - return new PriviAction(GET_SECURITY_PROPERTY, property); + public static PrivilegedAction getSecurityProperty(String property) { + return new PriviAction(GET_SECURITY_PROPERTY, property); } private PriviAction(int action, Object arg) {