changeset 24041:2bb2c08c958b

8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy Reviewed-by: mduigou Contributed-by: bernd-2014@eckenfels.net, claes.redestad@oracle.com
author mduigou
date Tue, 22 Apr 2014 10:38:20 -0700
parents 314ab00b353c
children 82290278c4af
files jdk/src/share/classes/sun/net/InetAddressCachePolicy.java
diffstat 1 files changed, 51 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/jdk/src/share/classes/sun/net/InetAddressCachePolicy.java	Tue Apr 22 16:45:10 2014 +0200
+++ b/jdk/src/share/classes/sun/net/InetAddressCachePolicy.java	Tue Apr 22 10:38:20 2014 -0700
@@ -84,19 +84,31 @@
      * Initialize
      */
     static {
-        Integer tmp = null;
 
-        try {
-            tmp = new Integer(
-              java.security.AccessController.doPrivileged (
-                new PrivilegedAction<String>() {
-                  public String run() {
-                      return Security.getProperty(cachePolicyProp);
-                  }
-              }));
-        } catch (NumberFormatException e) {
-            // ignore
-        }
+        Integer tmp = java.security.AccessController.doPrivileged(
+          new PrivilegedAction<Integer>() {
+            public Integer run() {
+                try {
+                    String tmpString = Security.getProperty(cachePolicyProp);
+                    if (tmpString != null) {
+                        return Integer.valueOf(tmpString);
+                    }
+                } catch (NumberFormatException ignored) {
+                    // Ignore
+                }
+
+                try {
+                    String tmpString = System.getProperty(cachePolicyPropFallback);
+                    if (tmpString != null) {
+                        return Integer.decode(tmpString);
+                    }
+                } catch (NumberFormatException ignored) {
+                    // Ignore
+                }
+                return null;
+            }
+          });
+
         if (tmp != null) {
             cachePolicy = tmp.intValue();
             if (cachePolicy < 0) {
@@ -104,35 +116,36 @@
             }
             propertySet = true;
         } else {
-            tmp = java.security.AccessController.doPrivileged
-                (new sun.security.action.GetIntegerAction(cachePolicyPropFallback));
-            if (tmp != null) {
-                cachePolicy = tmp.intValue();
-                if (cachePolicy < 0) {
-                    cachePolicy = FOREVER;
-                }
-                propertySet = true;
-            } else {
-                /* No properties defined for positive caching. If there is no
-                 * security manager then use the default positive cache value.
-                 */
-                if (System.getSecurityManager() == null) {
-                    cachePolicy = DEFAULT_POSITIVE;
-                }
+            /* No properties defined for positive caching. If there is no
+             * security manager then use the default positive cache value.
+             */
+            if (System.getSecurityManager() == null) {
+                cachePolicy = DEFAULT_POSITIVE;
             }
         }
+        tmp = java.security.AccessController.doPrivileged (
+          new PrivilegedAction<Integer>() {
+            public Integer run() {
+                try {
+                    String tmpString = Security.getProperty(negativeCachePolicyProp);
+                    if (tmpString != null) {
+                        return Integer.valueOf(tmpString);
+                    }
+                } catch (NumberFormatException ignored) {
+                    // Ignore
+                }
 
-        try {
-            tmp = new Integer(
-              java.security.AccessController.doPrivileged (
-                new PrivilegedAction<String>() {
-                  public String run() {
-                      return Security.getProperty(negativeCachePolicyProp);
-                  }
-              }));
-        } catch (NumberFormatException e) {
-            // ignore
-        }
+                try {
+                    String tmpString = System.getProperty(negativeCachePolicyPropFallback);
+                    if (tmpString != null) {
+                        return Integer.decode(tmpString);
+                    }
+                } catch (NumberFormatException ignored) {
+                    // Ignore
+                }
+                return null;
+            }
+          });
 
         if (tmp != null) {
             negativeCachePolicy = tmp.intValue();
@@ -140,16 +153,6 @@
                 negativeCachePolicy = FOREVER;
             }
             propertyNegativeSet = true;
-        } else {
-            tmp = java.security.AccessController.doPrivileged
-                (new sun.security.action.GetIntegerAction(negativeCachePolicyPropFallback));
-            if (tmp != null) {
-                negativeCachePolicy = tmp.intValue();
-                if (negativeCachePolicy < 0) {
-                    negativeCachePolicy = FOREVER;
-                }
-                propertyNegativeSet = true;
-            }
         }
     }