Skip to content

Commit bb7ce77

Browse files
committed
backport 4c695fa8a459adcdb8cdfe9e90783007c65fb90e
1 parent 0f2ffb5 commit bb7ce77

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -28,7 +28,6 @@
2828
import jdk.internal.math.DoubleToDecimal;
2929
import jdk.internal.math.FloatToDecimal;
3030

31-
import java.io.IOException;
3231
import java.nio.CharBuffer;
3332
import java.util.Arrays;
3433
import java.util.Spliterator;
@@ -356,8 +355,12 @@ public void setLength(int newLength) {
356355
*/
357356
@Override
358357
public char charAt(int index) {
358+
byte coder = this.coder;
359+
byte[] value = this.value;
360+
// Ensure count is less than or equal to capacity (racy reads and writes can produce inconsistent values)
361+
int count = Math.min(this.count, value.length >> coder);
359362
checkIndex(index, count);
360-
if (isLatin1()) {
363+
if (coder == LATIN1) {
361364
return (char)(value[index] & 0xff);
362365
}
363366
return StringUTF16.getChar(value, index);
@@ -416,6 +419,7 @@ public int codePointAt(int index) {
416419
* of this sequence.
417420
*/
418421
public int codePointBefore(int index) {
422+
byte[] value = this.value;
419423
int i = index - 1;
420424
checkIndex(i, count);
421425
if (isLatin1()) {
@@ -1720,7 +1724,7 @@ private final void putCharsAt(int index, CharSequence s, int off, int end) {
17201724
} else {
17211725
inflate();
17221726
// store c to make sure it has a UTF16 char
1723-
StringUTF16.putChar(this.value, j++, c);
1727+
StringUTF16.putCharSB(this.value, j++, c);
17241728
i++;
17251729
StringUTF16.putCharsSB(this.value, j, s, i, end);
17261730
return;
@@ -1810,7 +1814,7 @@ private final void appendChars(CharSequence s, int off, int end) {
18101814
count = j;
18111815
inflate();
18121816
// Store c to make sure sb has a UTF16 char
1813-
StringUTF16.putChar(this.value, j++, c);
1817+
StringUTF16.putCharSB(this.value, j++, c);
18141818
count = j;
18151819
i++;
18161820
StringUTF16.putCharsSB(this.value, j, s, i, end);

0 commit comments

Comments
 (0)