# HG changeset patch # User igerasim # Date 1456167313 -10800 # Mon Feb 22 21:55:13 2016 +0300 # Node ID 94b1fcb32e96d932558dfefcf27d72a63c0eb7f1 # Parent b9b28d7137cd2080c5ee97323315ca4aa0405486 [mq]: 8149330-Friendly-realloc-in-StringBuilder diff --git a/src/java.base/share/classes/java/lang/AbstractStringBuilder.java b/src/java.base/share/classes/java/lang/AbstractStringBuilder.java --- a/src/java.base/share/classes/java/lang/AbstractStringBuilder.java +++ b/src/java.base/share/classes/java/lang/AbstractStringBuilder.java @@ -145,6 +145,14 @@ } /** + * The maximum size of array to allocate (unless necessary). + * 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; + + /** * This implements the expansion semantics of ensureCapacity with no * size check or synchronization. */ @@ -153,17 +161,15 @@ if (newCapacity - minimumCapacity < 0) { newCapacity = minimumCapacity; } - if (newCapacity < 0) { - if (minimumCapacity < 0) {// overflow + + final int SAFE_BOUND = (MAX_ARRAY_SIZE >> coder); + if (((SAFE_BOUND - newCapacity) | newCapacity) < 0) { + final int UNSAFE_BOUND = (Integer.MAX_VALUE >> coder); + if (UNSAFE_BOUND - minimumCapacity < 0) { // overflow throw new OutOfMemoryError(); } - newCapacity = Integer.MAX_VALUE; - } - if (coder != LATIN1 && newCapacity > StringUTF16.MAX_LENGTH) { - if (minimumCapacity >= StringUTF16.MAX_LENGTH) { - throw new OutOfMemoryError(); - } - newCapacity = StringUTF16.MAX_LENGTH; + newCapacity = (minimumCapacity > SAFE_BOUND) + ? minimumCapacity : SAFE_BOUND; } this.value = Arrays.copyOf(value, newCapacity << coder); } diff --git a/test/java/lang/StringBuilder/HugeCapacity.java b/test/java/lang/StringBuilder/HugeCapacity.java new file mode 100644 --- /dev/null +++ b/test/java/lang/StringBuilder/HugeCapacity.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * @test + * @bug 8149330 + * @summary Capacity should not get close to Integer.MAX_VALUE unless + * necessary + * @run main/othervm -Xmx5G HugeCapacity + * @ignore This test has huge memory requirements + */ + +public class HugeCapacity { + private static int failures = 0; + + public static void main(String[] args) { + testLatin1(); + testUtf8(); + if (failures > 0) { + throw new RuntimeException(failures + " tests failed"); + } + } + + private static void testLatin1() { + try { + StringBuilder sb = new StringBuilder(); + sb.ensureCapacity(Integer.MAX_VALUE / 2); + sb.ensureCapacity(Integer.MAX_VALUE / 2 + 1); + } catch (OutOfMemoryError oom) { + oom.printStackTrace(); + failures++; + } + } + + private static void testUtf8() { + try { + StringBuilder sb = new StringBuilder(); + sb.append('\u042b'); + sb.ensureCapacity(Integer.MAX_VALUE / 4); + sb.ensureCapacity(Integer.MAX_VALUE / 4 + 1); + } catch (OutOfMemoryError oom) { + oom.printStackTrace(); + failures++; + } + } +}