-
Bug
-
Resolution: Fixed
-
P3
-
21, 22
-
b05
-
generic
-
generic
-
Verified
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8324410 | 22.0.1 | Jim Laskey | P3 | Resolved | Fixed | b02 |
JDK-8324051 | 22 | Jim Laskey | P3 | Resolved | Fixed | b32 |
JDK-8323749 | 21.0.3 | Aleksey Shipilev | P3 | Resolved | Fixed | b01 |
This issue exists for both StringBuffer.repeat(CharSequence cs, int count) and StringBuffer.repeat(int codePoint, int count).
When the method StringBuffer.repeat(CharSequence cs, int count) is invoked with parameter count >= 2 after toString() method was invoked, it does not append the given character sequence repeatedly to the string buffer as expected. The method StringBuffer.repeat(int codePoint, int count) does not work with any count >= 1 after invocation of toString() either.
This issue does not occur for StringBuilder.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run this code with JDK version 21.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
This code should print the following result:
abb
abb
ab
ACTUAL -
This code prints the following result:
a
a
a
---------- BEGIN SOURCE ----------
public class Main {
public static void main(String[] args) {
test1();
test2();
test3();
}
private static void test1() {
var sbf = new StringBuffer("a");
sbf.toString(); // necessary to produce the bug
sbf.repeat("b", 2);
System.out.println(sbf); // implicit toString() call
}
private static void test2() {
var sbf = new StringBuffer("a");
sbf.toString();
sbf.repeat('b', 2);
System.out.println(sbf);
}
private static void test3() {
var sbf = new StringBuffer("a");
sbf.toString();
sbf.repeat('b', 1);
System.out.println(sbf);
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Replace StringBuffer with StringBuilder (since StringBuffer is not widely used in modern Java programming). Or try to invoke toString() only once.
FREQUENCY : always
- backported by
-
JDK-8323749 StringBuffer.repeat does not work correctly after toString() was called
- Resolved
-
JDK-8324051 StringBuffer.repeat does not work correctly after toString() was called
- Resolved
-
JDK-8324410 StringBuffer.repeat does not work correctly after toString() was called
- Resolved
- duplicates
-
JDK-8324810 String.repeat() not working correctly in JShell
- Closed
- relates to
-
JDK-8302323 Add repeat methods to StringBuilder/StringBuffer
- Resolved
- links to
-
Commit openjdk/jdk21u-dev/6ee8f477
-
Commit openjdk/jdk22/60c68a13
-
Commit openjdk/jdk/df22fb32
-
Review openjdk/jdk21u-dev/143
-
Review openjdk/jdk22/87
-
Review openjdk/jdk/17172