src/java.base/share/classes/java/util/Properties.java
changeset 47423 4fc2a4a29f3d
parent 47216 71c04702a3d5
child 47476 1851856462b0
--- a/src/java.base/share/classes/java/util/Properties.java	Wed Jul 26 17:44:06 2017 +0100
+++ b/src/java.base/share/classes/java/util/Properties.java	Wed Aug 02 10:34:35 2017 -0700
@@ -42,6 +42,7 @@
 import java.util.function.BiFunction;
 import java.util.function.Function;
 
+import jdk.internal.misc.SharedSecrets;
 import jdk.internal.util.xml.PropertiesDefaultHandler;
 
 /**
@@ -1441,6 +1442,16 @@
             throw new StreamCorruptedException("Illegal # of Elements: " + elements);
         }
 
+        // Constructing the backing map will lazily create an array when the first element is
+        // added, so check it before construction. Note that CHM's constructor takes a size
+        // that is the number of elements to be stored -- not the table size -- so it must be
+        // inflated by the default load factor of 0.75, then inflated to the next power of two.
+        // (CHM uses the same power-of-two computation as HashMap, and HashMap.tableSizeFor is
+        // accessible here.) Check Map.Entry[].class since it's the nearest public type to
+        // what is actually created.
+        SharedSecrets.getJavaObjectInputStreamAccess()
+                     .checkArray(s, Map.Entry[].class, HashMap.tableSizeFor((int)(elements / 0.75)));
+
         // create CHM of appropriate capacity
         map = new ConcurrentHashMap<>(elements);