diff --git a/jdk/src/share/classes/java/util/HashSet.java b/jdk/src/share/classes/java/util/HashSet.java
index 6ab58d0edf8..f9b09ee4c83 100644
--- a/jdk/src/share/classes/java/util/HashSet.java
+++ b/jdk/src/share/classes/java/util/HashSet.java
@@ -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
@@ -294,16 +296,37 @@ public class HashSet
// 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!");
+ }
+ }
+}