From b477a7dcadec2add72b214cbf20c6772e8e201b4 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sun, 25 Jun 2017 13:37:15 +0300 Subject: [PATCH] Fix capacity calculation in ArrayList and StringBuilder. See #289 and #290 --- .../org/teavm/classlib/java/lang/TAbstractStringBuilder.java | 5 ++++- .../main/java/org/teavm/classlib/java/util/TArrayList.java | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java index 8c9e22f0b..92aeaafb5 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java @@ -581,7 +581,10 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ if (buffer.length >= capacity) { return; } - buffer = TArrays.copyOf(buffer, capacity * 2 + 1); + int newLength = buffer.length < Integer.MAX_VALUE / 2 + ? Math.max(capacity, Math.max(buffer.length * 2, 5)) + : Integer.MAX_VALUE; + buffer = TArrays.copyOf(buffer, newLength); } public void trimToSize() { diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TArrayList.java b/classlib/src/main/java/org/teavm/classlib/java/util/TArrayList.java index 823a5f1ca..614f35921 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TArrayList.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TArrayList.java @@ -53,7 +53,10 @@ public class TArrayList extends TAbstractList implements TCloneable, TSeri public void ensureCapacity(int minCapacity) { if (array.length < minCapacity) { - array = TArrays.copyOf(array, array.length + TMath.max(5, array.length / 2)); + int newLength = array.length < Integer.MAX_VALUE / 2 + ? Math.max(minCapacity, Math.max(array.length * 2, 5)) + : Integer.MAX_VALUE; + array = TArrays.copyOf(array, newLength); } }