Skip to content

Commit 052e252

Browse files
committed
8356152: String.concat can throw StringIndexOutOfBoundsException
Reviewed-by: liach, aturbanov, redestad, rriggs
1 parent b21b3a3 commit 052e252

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/java.base/share/classes/java/lang/StringConcatHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ static String simpleConcat(Object first, Object second) {
432432
@ForceInline
433433
static String doConcat(String s1, String s2) {
434434
byte coder = (byte) (s1.coder() | s2.coder());
435-
int newLength = (s1.length() + s2.length()) << coder;
435+
int newLength = checkOverflow(s1.length() + s2.length()) << coder;
436436
byte[] buf = newArray(newLength);
437437
s1.getBytes(buf, 0, coder);
438438
s2.getBytes(buf, s1.length(), coder);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8356152
27+
* @summary Check that huge concatenations throw OutOfMemoryError
28+
* @requires os.maxMemory > 8G
29+
* @run junit/othervm -Xmx8G -XX:+CompactStrings -Dcompact=true HugeConcatTest
30+
* @run junit/othervm -Xmx8G -XX:-CompactStrings -Dcompact=false HugeConcatTest
31+
*/
32+
33+
import org.junit.jupiter.api.BeforeAll;
34+
import org.junit.jupiter.api.Test;
35+
36+
import static org.junit.Assert.assertThrows;
37+
38+
public class HugeConcatTest {
39+
40+
private static final int HUGE_LENGTH_UTF16 = Integer.MAX_VALUE / 2 - 2;
41+
42+
private static String hugeLatin1;
43+
private static final String hugeUTF16 = "\u20AC".repeat(HUGE_LENGTH_UTF16);
44+
45+
@BeforeAll
46+
public static void initHugeLatin1() {
47+
String compact = System.getProperty("compact", "true");
48+
int hugeLatin1Length = Boolean.parseBoolean(compact)
49+
? Integer.MAX_VALUE - 2
50+
: HUGE_LENGTH_UTF16;
51+
hugeLatin1 = "a".repeat(hugeLatin1Length);
52+
}
53+
54+
@Test
55+
public void testConcat_Latin1_Latin1() {
56+
assertThrows(OutOfMemoryError.class, () -> { var s = hugeLatin1 + hugeLatin1; });
57+
assertThrows(OutOfMemoryError.class, () -> hugeLatin1.concat(hugeLatin1));
58+
}
59+
60+
@Test
61+
public void testConcat_Latin1_UTF16() {
62+
assertThrows(OutOfMemoryError.class, () -> { var s = hugeLatin1 + hugeUTF16; });
63+
assertThrows(OutOfMemoryError.class, () -> hugeLatin1.concat(hugeUTF16));
64+
}
65+
66+
@Test
67+
public void testConcat_UTF16_Latin1() {
68+
assertThrows(OutOfMemoryError.class, () -> { var s = hugeUTF16 + hugeLatin1; });
69+
assertThrows(OutOfMemoryError.class, () -> hugeUTF16.concat(hugeLatin1));
70+
}
71+
72+
@Test
73+
public void testConcat_UTF16_UTF16() {
74+
assertThrows(OutOfMemoryError.class, () -> { var s = hugeUTF16 + hugeUTF16; });
75+
assertThrows(OutOfMemoryError.class, () -> hugeUTF16.concat(hugeUTF16));
76+
}
77+
78+
}

0 commit comments

Comments
 (0)