aboutsummaryrefslogtreecommitdiff
path: root/src/share/classes/java/util/Vector.java
diff options
context:
space:
mode:
authormartin <none@none>2010-05-09 00:59:30 -0700
committermartin <none@none>2010-05-09 00:59:30 -0700
commitccddee00ecfc31310c0a1e6289db8e5cbd73c60e (patch)
tree0c13b1c9fdd12a73861b442801d30f13a48f414d /src/share/classes/java/util/Vector.java
parentfbf5ab7d24b9bb2d4f18d225e521e58a16bcec27 (diff)
downloadjdk8u_jdk-ccddee00ecfc31310c0a1e6289db8e5cbd73c60e.tar.gz
6933217: Huge arrays handled poorly in core libraries
Summary: Write overflow-conscious array resizing code Reviewed-by: chegar
Diffstat (limited to 'src/share/classes/java/util/Vector.java')
-rw-r--r--src/share/classes/java/util/Vector.java39
1 files changed, 30 insertions, 9 deletions
diff --git a/src/share/classes/java/util/Vector.java b/src/share/classes/java/util/Vector.java
index f70d36ee06..6fa6c84f8d 100644
--- a/src/share/classes/java/util/Vector.java
+++ b/src/share/classes/java/util/Vector.java
@@ -235,16 +235,37 @@ public class Vector<E>
* @see #ensureCapacity(int)
*/
private void ensureCapacityHelper(int minCapacity) {
+ // overflow-conscious code
+ if (minCapacity - elementData.length > 0)
+ grow(minCapacity);
+ }
+
+ /**
+ * The maximum size of array to allocate.
+ * Some VMs reserve some header words in an array.
+ * Attempts to allocate larger arrays may result in
+ * OutOfMemoryError: Requested array size exceeds VM limit
+ */
+ private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
+
+ private void grow(int minCapacity) {
+ // overflow-conscious code
int oldCapacity = elementData.length;
- if (minCapacity > oldCapacity) {
- Object[] oldData = elementData;
- int newCapacity = (capacityIncrement > 0) ?
- (oldCapacity + capacityIncrement) : (oldCapacity * 2);
- if (newCapacity < minCapacity) {
- newCapacity = minCapacity;
- }
- elementData = Arrays.copyOf(elementData, newCapacity);
- }
+ int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
+ capacityIncrement : oldCapacity);
+ if (newCapacity - minCapacity < 0)
+ newCapacity = minCapacity;
+ if (newCapacity - MAX_ARRAY_SIZE > 0)
+ newCapacity = hugeCapacity(minCapacity);
+ elementData = Arrays.copyOf(elementData, newCapacity);
+ }
+
+ private static int hugeCapacity(int minCapacity) {
+ if (minCapacity < 0) // overflow
+ throw new OutOfMemoryError();
+ return (minCapacity > MAX_ARRAY_SIZE) ?
+ Integer.MAX_VALUE :
+ MAX_ARRAY_SIZE;
}
/**