-
Enhancement
-
Resolution: Unresolved
-
P5
-
None
-
None
-
generic
-
generic
A DESCRIPTION OF THE PROBLEM :
When calling the String.getChars method, if the capacity of the destination character array passed in is smaller than the number of characters to be copied, the method incorrectly throws a StringIndexOutOfBoundsException instead of the ArrayIndexOutOfBoundsException expected based on the official documentation and logical reasoning. This issue can be observed across multiple JDK versions (e.g., JDK 11, 17, 21, etc.) and may be a potential implementation bug. Below are the JDK 11 documentation links for reference:
StringIndexOutOfBoundsException: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/StringIndexOutOfBoundsException.html
ArrayIndexOutOfBoundsException: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ArrayIndexOutOfBoundsException.html
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
root@3a4475348ea4:~/example# /root/hotspot/jdk-11.0.25/bin/javac /root/example/test.java
root@3a4475348ea4:~/example# /root/hotspot/jdk-11.0.25/bin/java test
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
java.lang.ArrayIndexOutOfBoundsException
ACTUAL -
java.lang.StringIndexOutOfBoundsException
---------- BEGIN SOURCE ----------
public class test {
public static void main(String[] args) {
char[] buffer = new char[10];
String longString = "This is a very long string that exceeds the buffer size";
System.out.println(longString.length());
// Attempt to copy the long string into the buffer
longString.getChars(0, longString.length(), buffer, 0);
}
}
---------- END SOURCE ----------
When calling the String.getChars method, if the capacity of the destination character array passed in is smaller than the number of characters to be copied, the method incorrectly throws a StringIndexOutOfBoundsException instead of the ArrayIndexOutOfBoundsException expected based on the official documentation and logical reasoning. This issue can be observed across multiple JDK versions (e.g., JDK 11, 17, 21, etc.) and may be a potential implementation bug. Below are the JDK 11 documentation links for reference:
StringIndexOutOfBoundsException: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/StringIndexOutOfBoundsException.html
ArrayIndexOutOfBoundsException: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ArrayIndexOutOfBoundsException.html
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
root@3a4475348ea4:~/example# /root/hotspot/jdk-11.0.25/bin/javac /root/example/test.java
root@3a4475348ea4:~/example# /root/hotspot/jdk-11.0.25/bin/java test
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
java.lang.ArrayIndexOutOfBoundsException
ACTUAL -
java.lang.StringIndexOutOfBoundsException
---------- BEGIN SOURCE ----------
public class test {
public static void main(String[] args) {
char[] buffer = new char[10];
String longString = "This is a very long string that exceeds the buffer size";
System.out.println(longString.length());
// Attempt to copy the long string into the buffer
longString.getChars(0, longString.length(), buffer, 0);
}
}
---------- END SOURCE ----------