Skip to content

Commit fde5b16

Browse files
Aleksei VoitylovRoger Riggs
Aleksei Voitylov
and
Roger Riggs
committedDec 14, 2023
8321514: UTF16 string gets constructed incorrectly from codepoints if CompactStrings is not enabled
Co-authored-by: Roger Riggs <rriggs@openjdk.org> Reviewed-by: rriggs
1 parent 45a9ade commit fde5b16

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed
 

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ public static byte[] toBytes(int[] val, int index, int len) {
415415
int n = computeCodePointSize(val, index, end);
416416

417417
byte[] buf = newBytesFor(n);
418-
return extractCodepoints(val, index, len, buf, 0);
418+
return extractCodepoints(val, index, end, buf, 0);
419419
}
420420

421421
public static byte[] toBytes(char c) {

‎test/jdk/java/lang/String/Chars.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323

2424
/*
2525
* @test
26-
* @bug 8054307 8311906
26+
* @bug 8054307 8311906 8321514
2727
* @summary test String chars() and codePoints()
28+
* @run main/othervm -XX:+CompactStrings Chars
29+
* @run main/othervm -XX:-CompactStrings Chars
2830
*/
2931

3032
import java.util.Arrays;
@@ -45,6 +47,7 @@ public static void main(String[] args) {
4547
}
4648
testChars(cc, ccExp);
4749
testCharsSubrange(cc, ccExp);
50+
testIntsSubrange(ccExp);
4851
testCPs(cc, cpExp);
4952

5053
// bmp without surrogates
@@ -72,6 +75,7 @@ public static void main(String[] args) {
7275
cpExp = Arrays.copyOf(cpExp, k);
7376
testChars(cc, ccExp);
7477
testCharsSubrange(cc, ccExp);
78+
testIntsSubrange(ccExp);
7579
testCPs(cc, cpExp);
7680
}
7781
}
@@ -104,6 +108,27 @@ static void testCharsSubrange(char[] cc, int[] expected) {
104108
}
105109
}
106110

111+
static void testIntsSubrange(int[] expected) {
112+
int[] offsets = { 7, 31 }; // offsets to test
113+
int LENGTH = 13;
114+
for (int i = 0; i < offsets.length; i++) {
115+
int offset = Math.max(0, offsets[i]); // confine to the input array
116+
int count = Math.min(LENGTH, expected.length - offset);
117+
String str = new String(expected, offset, count);
118+
int[] actual = str.chars().toArray();
119+
int errOffset = Arrays.mismatch(actual, 0, actual.length,
120+
expected, offset, offset + count);
121+
if (errOffset >= 0) {
122+
System.err.printf("expected[%d] (%d) != actual[%d] (%d)%n",
123+
offset + errOffset, expected[offset + errOffset],
124+
errOffset, actual[errOffset]);
125+
System.err.println("expected: " + Arrays.toString(expected));
126+
System.err.println("actual: " + Arrays.toString(actual));
127+
throw new RuntimeException("testIntsSubrange failed!");
128+
}
129+
}
130+
}
131+
107132
static void testCPs(char[] cc, int[] expected) {
108133
String str = new String(cc);
109134
if (!Arrays.equals(expected, str.codePoints().toArray())) {

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Dec 14, 2023

@openjdk-notifier[bot]
Please sign in to comment.