Skip to content

Commit

Permalink
8320570: NegativeArraySizeException decoding >1G UTF8 bytes with non-…
Browse files Browse the repository at this point in the history
…ascii characters

Reviewed-by: phh
Backport-of: 82796bdebbf56b98ec97a6d572ed68c2842f60c6
  • Loading branch information
Amos Shi committed Apr 9, 2024
1 parent c430b11 commit fc01ffe
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/java.base/share/classes/java/lang/String.java
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,9 @@ public String(byte[] bytes, int offset, int length, Charset charset) {
}
}
if (dp == 0 || dst == null) {
dst = new byte[length << 1];
dst = StringUTF16.newBytesFor(length);
} else {
byte[] buf = new byte[length << 1];
byte[] buf = StringUTF16.newBytesFor(length);
StringLatin1.inflate(dst, 0, buf, 0, dp);
dst = buf;
}
Expand All @@ -589,7 +589,7 @@ public String(byte[] bytes, int offset, int length, Charset charset) {
this.value = Arrays.copyOfRange(bytes, offset, offset + length);
this.coder = LATIN1;
} else {
byte[] dst = new byte[length << 1];
byte[] dst = StringUTF16.newBytesFor(length);
int dp = 0;
while (dp < length) {
int b = bytes[offset++];
Expand Down Expand Up @@ -723,9 +723,9 @@ static String newStringUTF8NoRepl(byte[] bytes, int offset, int length) {
}
}
if (dp == 0 || dst == null) {
dst = new byte[length << 1];
dst = StringUTF16.newBytesFor(length);
} else {
byte[] buf = new byte[length << 1];
byte[] buf = StringUTF16.newBytesFor(length);
StringLatin1.inflate(dst, 0, buf, 0, dp);
dst = buf;
}
Expand Down Expand Up @@ -1267,7 +1267,7 @@ private static byte[] encodeUTF8(byte coder, byte[] val, boolean doReplace) {
return Arrays.copyOf(val, val.length);

int dp = 0;
byte[] dst = new byte[val.length << 1];
byte[] dst = StringUTF16.newBytesFor(val.length);
for (byte c : val) {
if (c < 0) {
dst[dp++] = (byte) (0xc0 | ((c & 0xff) >> 6));
Expand Down
76 changes: 76 additions & 0 deletions test/jdk/java/lang/String/CompactString/NegativeSize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2023, 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.
*/

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/*
* @test
* @bug 8077559
* @summary Tests Compact String for negative size.
* @requires vm.bits == 64 & os.maxMemory >= 4G
* @compile -encoding utf-8 NegativeSize.java
* @run main/othervm -XX:+CompactStrings -Xmx4g NegativeSize
* @run main/othervm -XX:-CompactStrings -Xmx4g NegativeSize
*/

// In Java8: java.lang.OutOfMemoryError: Java heap space
// In Java9+: was java.lang.NegativeArraySizeException: -1894967266
public class NegativeSize {

static byte[] generateData() {
int asciisize = 1_200_000_000;
byte[] nonAscii = "非アスキー".getBytes();
int nonAsciiSize = nonAscii.length;
// 1 GB
byte[] arr = new byte[asciisize + nonAsciiSize];
for (int i=0; i<asciisize; ++i) {
arr[i] = (byte)('0' + (i % 40));
}
for(int i=0; i<nonAsciiSize; ++i) {
arr[i + asciisize] = nonAscii[i];
}
return arr;
}


public static void main(String[] args) throws IOException {

try {
byte[] largeBytes = generateData();
String inStr = new String(largeBytes, StandardCharsets.UTF_8);
System.out.println(inStr.length());
System.out.println(inStr.substring(1_200_000_000));
} catch (OutOfMemoryError ex) {
if (ex.getMessage().startsWith("UTF16 String size is")) {
System.out.println("Succeeded with OutOfMemoryError");
} else {
throw new RuntimeException("Failed: Not the OutOfMemoryError expected", ex);
}
} catch (NegativeArraySizeException ex) {
throw new RuntimeException("Failed: Expected OutOfMemoryError", ex);
}
}
}


1 comment on commit fc01ffe

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.