# HG changeset patch # User bpb # Date 1511465606 0 # Node ID 72f71c6b2bbc077bc160cdd0243e6cf612a6a018 # Parent 4fcf12d3efe19d2c9134c79a7251a7f655db0b1a 8016252: More defensive HashSet.readObject Summary: Add data validation checks in readObject(). Reviewed-by: alanb, mduigou, chegar Contributed-by: Brian Burkhalter diff -r 4fcf12d3efe1 -r 72f71c6b2bbc src/share/classes/java/util/HashSet.java --- a/src/share/classes/java/util/HashSet.java Thu Nov 23 03:37:33 2017 +0000 +++ b/src/share/classes/java/util/HashSet.java Thu Nov 23 19:33:26 2017 +0000 @@ -25,6 +25,8 @@ package java.util; +import java.io.InvalidObjectException; + /** * This class implements the Set interface, backed by a hash table * (actually a HashMap instance). It makes no guarantees as to the @@ -293,16 +295,37 @@ // Read in any hidden serialization magic s.defaultReadObject(); - // Read in HashMap capacity and load factor and create backing HashMap + // Read capacity and verify non-negative. int capacity = s.readInt(); + if (capacity < 0) { + throw new InvalidObjectException("Illegal capacity: " + + capacity); + } + + // Read load factor and verify positive and non NaN. float loadFactor = s.readFloat(); + if (loadFactor <= 0 || Float.isNaN(loadFactor)) { + throw new InvalidObjectException("Illegal load factor: " + + loadFactor); + } + + // Read size and verify non-negative. + int size = s.readInt(); + if (size < 0) { + throw new InvalidObjectException("Illegal size: " + + size); + } + + // Set the capacity according to the size and load factor ensuring that + // the HashMap is at least 25% full but clamping to maximum capacity. + capacity = (int) Math.min(size * Math.min(1 / loadFactor, 4.0f), + HashMap.MAXIMUM_CAPACITY); + + // Create backing HashMap map = (((HashSet)this) instanceof LinkedHashSet ? new LinkedHashMap(capacity, loadFactor) : new HashMap(capacity, loadFactor)); - // Read in size - int size = s.readInt(); - // Read in all elements in the proper order. for (int i=0; i createHashSet() { + int capacity = rnd.nextInt(MAX_CAPACITY); + float loadFactor = Float.MIN_VALUE + rnd.nextFloat()*MAX_LOAD_FACTOR; + HashSet hashSet = new HashSet(capacity, loadFactor); + float multiplier = 2*rnd.nextFloat(); // range [0,2] + int size = (int)(capacity*loadFactor*multiplier); + for (int i = 0; i < size; i++) { + hashSet.add(rnd.nextInt()); + } + return hashSet; + } + + private static HashSet serDeser(HashSet hashSet) throws IOException, ClassNotFoundException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(hashSet); + oos.flush(); + + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ObjectInputStream ois = new ObjectInputStream(bais); + HashSet result = (HashSet)ois.readObject(); + + oos.close(); + ois.close(); + + return result; + } + + private static void printHashSet(HashSet hashSet) { + System.err.println("Size: "+hashSet.size()); + for (Object o : hashSet) { + System.err.println(o); + } + } + + public static void main(String[] args) { + int failures = 0; + + for (int i = 0; i < NUM_SETS; i++) { + HashSet hashSet = createHashSet(); + + HashSet result = null; + try { + result = serDeser(hashSet); + } catch (IOException ioe) { + System.err.println(ioe); + failures++; + } catch (ClassNotFoundException cnfe) { + System.err.println(cnfe); + failures++; + } + + if (!hashSet.equals(result)) { + System.err.println("Unequal HashSets!"); + printHashSet(hashSet); + System.err.println(); + failures++; + } + } + + if (failures != 0) { + throw new RuntimeException("HashSet/Serialzation failed with "+ + failures+" failures!"); + } + } +}