Skip to content

Commit 4fb5c12

Browse files
author
Roger Riggs
committed
8321180: Condition for non-latin1 string size too large exception is off by one
Reviewed-by: rgiulietti
1 parent d5a96e3 commit 4fb5c12

File tree

2 files changed

+111
-9
lines changed

2 files changed

+111
-9
lines changed

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,10 @@
4242

4343
final class StringUTF16 {
4444

45+
// Return a new byte array for a UTF16-coded string for len chars
46+
// Throw an exception if out of range
4547
public static byte[] newBytesFor(int len) {
46-
if (len < 0) {
47-
throw new NegativeArraySizeException();
48-
}
49-
if (len > MAX_LENGTH) {
50-
throw new OutOfMemoryError("UTF16 String size is " + len +
51-
", should be less than " + MAX_LENGTH);
52-
}
53-
return new byte[len << 1];
48+
return new byte[newBytesLength(len)];
5449
}
5550

5651
// Check the size of a UTF16-coded string
@@ -59,7 +54,7 @@ public static int newBytesLength(int len) {
5954
if (len < 0) {
6055
throw new NegativeArraySizeException();
6156
}
62-
if (len > MAX_LENGTH) {
57+
if (len >= MAX_LENGTH) {
6358
throw new OutOfMemoryError("UTF16 String size is " + len +
6459
", should be less than " + MAX_LENGTH);
6560
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (c) 2023, 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+
import org.junit.jupiter.api.Test;
25+
import static org.junit.jupiter.api.Assertions.*;
26+
27+
import java.nio.charset.StandardCharsets;
28+
29+
/*
30+
* @test
31+
* @bug 8077559 8321180
32+
* @summary Tests Compact String for maximum size strings
33+
* @requires os.maxMemory >= 7g & vm.bits == 64
34+
* @run junit/othervm -XX:+CompactStrings -Xmx7g MaxSizeUTF16String
35+
* @run junit/othervm -XX:-CompactStrings -Xmx7g MaxSizeUTF16String
36+
*/
37+
38+
public class MaxSizeUTF16String {
39+
40+
private final static int MAX_UTF16_STRING_LENGTH = Integer.MAX_VALUE / 2;
41+
42+
private final static String EXPECTED_OOME_MESSAGE = "UTF16 String size is";
43+
44+
// Create a large UTF-8 byte array with a single non-latin1 character
45+
private static byte[] generateUTF8Data(int byteSize) {
46+
byte[] nonAscii = "\u0100".getBytes(StandardCharsets.UTF_8);
47+
byte[] arr = new byte[byteSize];
48+
System.arraycopy(nonAscii, 0, arr, 0, nonAscii.length); // non-latin1 at start
49+
return arr;
50+
}
51+
52+
// Create a large char array with a single non-latin1 character
53+
private static char[] generateCharData(int size) {
54+
char[] nonAscii = "\u0100".toCharArray();
55+
char[] arr = new char[size];
56+
System.arraycopy(nonAscii, 0, arr, 0, nonAscii.length); // non-latin1 at start
57+
return arr;
58+
}
59+
60+
@Test
61+
public void testMaxUTF8() {
62+
// Overly large UTF-8 data with 1 non-latin1 char
63+
final byte[] large_utf8_bytes = generateUTF8Data(MAX_UTF16_STRING_LENGTH + 1);
64+
int[] sizes = new int[] {
65+
MAX_UTF16_STRING_LENGTH + 1,
66+
MAX_UTF16_STRING_LENGTH,
67+
MAX_UTF16_STRING_LENGTH - 1};
68+
for (int size : sizes) {
69+
System.err.println("Checking max UTF16 string len: " + size);
70+
try {
71+
// Use only part of the UTF-8 byte array
72+
new String(large_utf8_bytes, 0, size, StandardCharsets.UTF_8);
73+
if (size >= MAX_UTF16_STRING_LENGTH) {
74+
fail("Expected OutOfMemoryError with message prefix: " + EXPECTED_OOME_MESSAGE);
75+
}
76+
} catch (OutOfMemoryError ex) {
77+
if (!ex.getMessage().startsWith(EXPECTED_OOME_MESSAGE)) {
78+
fail("Failed: Not the OutOfMemoryError expected", ex);
79+
}
80+
}
81+
}
82+
}
83+
84+
@Test
85+
public void testMaxCharArray() {
86+
// Overly large UTF-8 data with 1 non-latin1 char
87+
final char[] large_char_array = generateCharData(MAX_UTF16_STRING_LENGTH + 1);
88+
int[] sizes = new int[]{
89+
MAX_UTF16_STRING_LENGTH + 1,
90+
MAX_UTF16_STRING_LENGTH,
91+
MAX_UTF16_STRING_LENGTH - 1};
92+
for (int size : sizes) {
93+
System.err.println("Checking max UTF16 string len: " + size);
94+
try {
95+
// Large char array with 1 non-latin1 char
96+
new String(large_char_array, 0, size);
97+
if (size >= MAX_UTF16_STRING_LENGTH) {
98+
fail("Expected OutOfMemoryError with message prefix: " + EXPECTED_OOME_MESSAGE);
99+
}
100+
} catch (OutOfMemoryError ex) {
101+
if (!ex.getMessage().startsWith(EXPECTED_OOME_MESSAGE)) {
102+
throw new RuntimeException("Wrong exception message: " + ex.getMessage(), ex);
103+
}
104+
}
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)