Fix HashMap and HashSet cloning problem
If HashMap or HashSet were cloned, it would no longer be equal to
"EMPTY_TABLE" causing the next insertion into the map to be inserted at
an index that was out of bounds.
Bug: 26294011
Change-Id: I374ed126d0d2fa725d6a7f86a6e419cb34ff9ce9
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/HashSetTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/HashSetTest.java
index ed9c596..4ff4b9d 100644
--- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/HashSetTest.java
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/HashSetTest.java
@@ -213,6 +213,15 @@
SerializationTest.verifyGolden(this, hs, comparator);
}
+ /*
+ * Bug 26294011
+ */
+ public void test_empty_clone() throws Exception {
+ HashSet<Integer> emptyHs = new HashSet<Integer>();
+ HashSet<Integer> cloned = (HashSet) emptyHs.clone();
+ cloned.add(new Integer(8));
+ }
+
/**
* Sets up the fixture, for example, open a network connection. This method
* is called before a test is executed.
diff --git a/ojluni/src/main/java/java/util/HashMap.java b/ojluni/src/main/java/java/util/HashMap.java
index 1ffa448..01b70a4 100755
--- a/ojluni/src/main/java/java/util/HashMap.java
+++ b/ojluni/src/main/java/java/util/HashMap.java
@@ -794,7 +794,14 @@
} catch (CloneNotSupportedException e) {
// assert false;
}
- result.table = new HashMapEntry[table.length];
+ if (result.table != EMPTY_TABLE) {
+ result.inflateTable(Math.min(
+ (int) Math.min(
+ size * Math.min(1 / loadFactor, 4.0f),
+ // we have limits...
+ HashMap.MAXIMUM_CAPACITY),
+ table.length));
+ }
result.entrySet = null;
result.modCount = 0;
result.size = 0;