JDK-6588260 : (str) ArrayIndexOutOfBoundsException when trying to create a String from codePoints
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2007-08-02
  • Updated: 2011-05-18
  • Resolved: 2011-05-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b19Fixed
Description
FULL PRODUCT VERSION :
java -version
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)

java version "1.5.0_11"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_11-b03)
Java HotSpot(TM) Client VM (build 1.5.0_11-b03, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Linux edwin-t-new 2.6.22.1-cfs-v19 #1 SMP PREEMPT Tue Jul 17 12:46:51 EEST 2007 i686 GNU/Linux


A DESCRIPTION OF THE PROBLEM :
When trying to create a string containing all codePoints (in range Character.MIN_CODE_POINT, Character.MAX_CODE_POINT) an ArrayIndexOutOfBounds exception is thrown from the String constructor.

Occurs with both java 1.5, and java 1.6.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the sample code below: javac Test.java
Run the sample code: java Test


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
No exceptions are thrown
ACTUAL -
Exception is thrown in the String constructor.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
When running with JDK 1.6.0:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1114113
        at java.lang.Character.toSurrogates(Character.java:2596)
        at java.lang.String.<init>(String.java:286)
        at Test.main(Test.java:7)

When running with JDK 1.5.0.11:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1114113
        at java.lang.Character.toSurrogates(Character.java:2596)
        at java.lang.String.<init>(String.java:275)
        at Test.main(Test.java:7)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
class Test {
public static void main(String[] args) {
        int [] codePoints=new int[Character.MAX_CODE_POINT];
        for(int i=0;i<codePoints.length;i++) {
            codePoints[i] = i;
        }
        final String original = new String(codePoints, 0, codePoints.length);

    }
}
---------- END SOURCE ----------

Comments
EVALUATION The constructor String(int[], int, int) has a complex and buggy implementation. Also, it does not guard against having the String backing array have trailing never-accessed chars. A more obvious implementation will be more clearly correct and clearly time- and space-efficient.
2007-08-03