United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6588260 : (str) ArrayIndexOutOfBoundsException when trying to create a String from codePoints

Details
Type:
Bug
Submit Date:
2007-08-02
Status:
Closed
Updated Date:
2011-05-18
Project Name:
JDK
Resolved Date:
2011-05-18
Component:
core-libs
OS:
linux
Sub-Component:
java.lang
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports

Sub Tasks

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



Hardware and Software, Engineered to Work Together