openj9 icon indicating copy to clipboard operation
openj9 copied to clipboard

OpenJ9 (JDK8) fails to throw the StringIndexOutOfBoundsException when calling function StringBuilder deleteCharAt (int index)

Open ayuan0828 opened this issue 2 years ago • 1 comments

Description:

When running the following test case, OpenJ9 (on JDK8) fails to throw an exception because the program deletes an element that does not exist shown at line 5. The expected result should be to catch the StringIndexOutOfBoundsException as OpenJ9 (on JDK 11) and HotSpot do.

Through debugging, I found when calling the function StringBuilder delete (int start, int end), OpenJ9 (JDK8) may incorrectly return the boundary condition value. Maybe it's the root cause.

Environment

Ubuntu 18.04 x86_64

JVM Information:

jdk8:

openjdk version "1.8.0_332-internal"

OpenJDK Runtime Environment (build 1.8.0_332-internal-_2022_02_17_10_23-b00)

Eclipse OpenJ9 VM (build master-39c490eaf, JRE 1.8.0 Linux amd64-64-Bit Compressed References 20220217_000000 (JIT enabled, AOT enabled)

OpenJ9  - 39c490eaf

OMR    - b63287860

JCL    - 301202f49f based on jdk8u332-b02)

jdk11:

openjdk version "11.0.15-internal" 2022-04-19

OpenJDK Runtime Environment (build 11.0.15-internal+0-adhoc..openj9-openjdk-jdk11)

Eclipse OpenJ9 VM (build master-39c490eaf, JRE 11 Linux amd64-64-Bit Compressed References 20220217_000000 (JIT enabled, AOT enabled)

OpenJ9  - 39c490eaf

OMR    - b63287860

JCL    - 24e7209ed4 based on jdk-11.0.15+1)

Testcase:

public class Test {

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

	StringBuilder sb = new StringBuilder();

	sb.append('J');

	sb.deleteCharAt(1);

	System.out.println(sb.toString());

  }

}

Output of JDK8:

J

Output of JDK11:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: index=1 length=1

at java.base/java.lang.String.checkIndex(String.java:112)

at java.base/java.lang.AbstractStringBuilder.deleteCharAt(AbstractStringBuilder.java:916)

at java.base/java.lang.StringBuilder.deleteCharAt(StringBuilder.java:297)

at Test.main(Test.java:5)

Output of HotSpot:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1

at java.lang.AbstractStringBuilder.deleteCharAt(AbstractStringBuilder.java:824)

at java.lang.StringBuilder.deleteCharAt(StringBuilder.java:253)

at Test.main(Test.java:5)

ayuan0828 avatar Sep 15 '22 11:09 ayuan0828

@tajila fyi

pshipton avatar Sep 15 '22 13:09 pshipton

@mikezhang1234567890 please take a look

tajila avatar Sep 25 '22 00:09 tajila

Looks like we're missing a boundary check in deleteCharAt, I'll add that in.

mikezhang1234567890 avatar Sep 26 '22 14:09 mikezhang1234567890