changeset 172:416d7f008dd6

8081383: NumberFormatException at DeviceMgmtPermission Summary: The class is redesigned Reviewed-by: snazarki Contributed-by: olga.milovskaya@oracle.com
author snazarki
date Thu, 28 May 2015 12:52:58 +0300
parents 36fd4f8f325a
children dfb0a1f399c7
files src/share/classes/jdk/dio/DeviceMgmtPermission.java
diffstat 1 files changed, 80 insertions(+), 97 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/jdk/dio/DeviceMgmtPermission.java	Mon May 25 18:45:07 2015 +0300
+++ b/src/share/classes/jdk/dio/DeviceMgmtPermission.java	Thu May 28 12:52:58 2015 +0300
@@ -26,10 +26,12 @@
 
 import java.security.Permission;
 import java.security.PermissionCollection;
+import java.util.Objects;
+import java.util.Vector;
+
 import com.oracle.dio.utils.ActionFactory;
-import java.util.Vector;
+import com.oracle.dio.utils.ActionFactory;
 import com.oracle.dio.utils.ExceptionMessage;
-import com.oracle.dio.utils.ActionFactory;
 
 /**
  * The {@code DeviceMgmtPermission} class defines permissions for registering and unregistering devices as
@@ -114,6 +116,10 @@
     /** Comma-separated ordered action list */
     private String myActions;
 
+    private String thisName;
+    private int lowID   = -1;
+    private int highID = -1;
+
     /**
      * Constructs a new {@code DeviceMgmtPermission} instance with the specified target name and action list.
      * The target name is normalized so that leading and trailing spaces are removed
@@ -142,9 +148,75 @@
                 ExceptionMessage.format(ExceptionMessage.DEVICE_NULL_ACTIONS)
             );
         }
+        checkTargetNameFormat(name);
         myActions = ActionFactory.verifyAndOrderActions(actions, REGISTER+","+UNREGISTER+","+OPEN);
     }
 
+    private void checkTargetNameFormat(String name) {
+
+        Objects.requireNonNull(name,  ExceptionMessage.format(ExceptionMessage.DEVICE_NULL_NAME));
+
+        String id;
+        int idx = -1;
+        while (-1 != (idx = name.indexOf(':', idx + 1))) {
+            if (idx == 0 || '\\' != name.charAt(idx - 1) ) {
+                break;
+            }
+        }
+
+        if (-1 == idx) {
+            thisName = name;
+            id = "";
+        } else {
+            thisName = name.substring(0, idx);
+            id = name.substring(idx + 1);
+            if ("".equals(id)) {
+                throw new IllegalArgumentException(ExceptionMessage.format(ExceptionMessage.DEVICE_INVALID_PERMISSION));
+            }
+        }
+
+        if ("*".equals(id) || "".equals(id)) {
+            lowID   = 0;
+            highID = Integer.MAX_VALUE;
+        } else {
+            idx = -1;
+            boolean foundDash = false;
+            for (int i = 0; i < id.length(); i++) {
+                char c = id.charAt(i);
+                if (!Character.isDigit(c)) {
+                    if ('-' == c && !foundDash) {
+                        foundDash = true;
+                        break;
+                    }
+                }
+            }
+
+            lowID   = 0;
+            highID = Integer.MAX_VALUE;
+            try {
+                if (foundDash) {
+                    idx = id.indexOf('-');
+                    if (idx > 0) {
+                        lowID = Integer.parseInt(id.substring(0, idx));
+                    }
+                    if (idx < id.length() - 1) {
+                        highID = Integer.parseInt(id.substring(idx + 1));
+                    }
+                    if (lowID > highID) {
+                        throw new IllegalArgumentException(ExceptionMessage.format(ExceptionMessage.DEVICE_INVALID_PERMISSION));
+                    }
+                } else {
+                    lowID = Integer.parseInt(id);
+                    highID = Integer.parseInt(id);
+                }
+            } catch (NumberFormatException e) {
+                throw new IllegalArgumentException(ExceptionMessage.format(ExceptionMessage.DEVICE_INVALID_PERMISSION));
+            }
+        }
+
+        return;
+    }
+
     /**
      * Checks two {@code DeviceMgmtPermission} objects for equality.
      * Checks that {@code obj}'s class is the same as this object's class and has the
@@ -205,42 +277,13 @@
      */
     @Override
     public boolean implies(Permission permission) {
+
         if ((permission == null) || (permission.getClass() != getClass()))
             return false;
 
         if (!ActionFactory.implies(myActions, permission.getActions())) return false;
 
-        int idx = -1;
-        String name = permission.getName();
-        while (-1 != (idx = name.indexOf(':', idx + 1))) {
-            if (idx == 0 || '\\' != name.charAt(idx - 1) ) {
-                break;
-            }
-        }
-        String thatName;
-        String thatID = "";
-        if (-1 == idx) {
-            thatName = name;
-        } else {
-            thatName = name.substring(0, idx);
-            thatID = name.substring(idx + 1);
-        }
-
-        idx = -1;
-        name = getName();
-        while (-1 != (idx = name.indexOf(':', idx + 1))) {
-            if (idx == 0 || '\\' != name.charAt(idx - 1) ) {
-                break;
-            }
-        }
-        String thisName;
-        String thisID = "";
-        if (-1 == idx) {
-            thisName = name;
-        } else {
-            thisName = name.substring(0, idx);
-            thisID = name.substring(idx + 1);
-        }
+        String thatName = ((DeviceMgmtPermission)permission).thisName;
 
         if (!"*".equals(thisName)) {
             // the empty string ("") designates an undefined peripheral name
@@ -251,71 +294,11 @@
             }
         }
 
-        if (!"*".equals(thisID) &&
-            !(thisID.length() == 0 && thatID.length() == 0)) {
-            boolean foundDash = false;
-            for (int i = 0; i < thisID.length(); i++) {
-                char c = thisID.charAt(i);
-                if (!Character.isDigit(c)) {
-                     if('-' == c && !foundDash) {
-                         foundDash = true;
-                         continue;
-                     }
-                } else {
-                    continue;
-                }
-                // invalid format
-                return false;
-            }
-            int thisLow = 0;
-            int thisHigh = Integer.MAX_VALUE;
-            if (foundDash) {
-                idx = thisID.indexOf('-');
-                if (idx > 0) {
-                    thisLow = Integer.parseInt(thisID.substring(0, idx));
-                }
-                if (idx < thisID.length() - 1) {
-                    thisHigh = Integer.parseInt(thisID.substring(idx + 1));
-                }
-            } else {
-                thisLow = thisHigh = Integer.parseInt(thisID);
-            }
+        int thatLowID   = ((DeviceMgmtPermission)permission).lowID;
+        int thatHightID = ((DeviceMgmtPermission)permission).highID;
 
-            foundDash = false;
-            for (int i = 0; i < thatID.length(); i++) {
-                char c = thatID.charAt(i);
-                if (!Character.isDigit(c)) {
-                     if('-' == c && !foundDash) {
-                         foundDash = true;
-                         continue;
-                     }
-                } else {
-                    continue;
-                }
-                // invalid format
-                return false;
-            }
-
-            int thatLow = 0;
-            int thatHigh = Integer.MAX_VALUE;
-            if (foundDash) {
-                idx = thatID.indexOf('-');
-                if (idx > 0) {
-                    thatLow = Integer.parseInt(thatID.substring(0, idx));
-                }
-                if (idx < thatID.length() - 1) {
-                    thatHigh = Integer.parseInt(thatID.substring(idx + 1));
-                }
-            } else {
-                thatLow = thatHigh = Integer.parseInt(thatID);
-            }
-
-            return (thatLow >= thisLow && thatLow <= thisHigh &&
-                thatHigh >= thisLow && thatHigh <= thisHigh);
-
-        }
-
-        return true;
+        return (thatLowID >= lowID && thatLowID <= highID &&
+                thatHightID >= lowID && thatHightID <= highID);
 
     }