Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 5045582
Votes 0
Synopsis (coll) binarySearch() fails for size larger than 1<<30
Category java:classes_util
Reported Against tiger-beta
Release Fixed mustang(b83)
State 10-Fix Delivered, Verified, bug
Priority: 2-High
Related Bugs 6412541 , 6437371 , 5050278 , 4306897
Submit Date 11-MAY-2004
Description


FULL PRODUCT VERSION :
java version "1.5.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b32c)
Java HotSpot(TM) Client VM (build 1.5.0-beta-b32c, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Linux freeway 2.4.21-4-686 #1 Sat Aug 2 23:27:25 EST 2003 i686 GNU/Linux


A DESCRIPTION OF THE PROBLEM :
java.util.Arrays.binarySearch() will throw an ArrayIndexOutOfBoundsException if the array
is large.  This is caused by overflow in the calculation:

           int mid = (low + high) >> 1;

The correct calculation uses unsigned shift:

           int mid = (low + high) >>> 1;

There are similar problems in Collections, and TreeMap also includes the faulty calculation

           int mid = (lo + hi) / 2;

There may be others.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the included code example.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The code should print

index = 1099999999

ACTUAL -
The code printed

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1064671149
        at java.util.Arrays.binarySearch(Arrays.java:1509)
        at infinitum.tom.Search.main(Search.java:7)


ERROR MESSAGES/STACK TRACES THAT OCCUR :
$ /jdk/j2sdk1.5.0/bin/java -Xmx1200m -server Search
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1064671149
        at java.util.Arrays.binarySearch(Arrays.java:1509)
        at infinitum.tom.Search.main(Search.java:7)


REPRODUCIBILITY :
This bug can be reproduced rarely.

---------- BEGIN SOURCE ----------
class Search {
    public static void main (String[] args) {
        byte[] b = new byte[1100000000];
        b[b.length - 1] = 1;
        int index = java.util.Arrays.binarySearch(b, (byte)1);
        System.out.println("index = " + index);
    }
}
---------- END SOURCE ----------
(Incident Review ID: 261136) 
======================================================================
Posted Date : 2006-04-15 19:58:40.0
Work Around
N/A
Evaluation
Should be fixed in the next release. Not for Tiger.
  xxxxx@xxxxx   2004-05-11
Finally fixing for Mustang.
"Can't even compute average of two ints" is pretty embarrassing.
Posted Date : 2006-04-18 18:04:16.0
Comments
  
  Include a link with my name & email   

Submitted On 03-JUN-2006
The Sun/Solaris look(1) command has the same problem.


Submitted On 03-JUN-2006
The Sun/Solaris look(1) command has exactly the same problem. For that reason it breaks for file larger than roughly 1GB


Submitted On 16-JUL-2007
This is reproduced in java version "1.5.0_12" on x86_64 Linux



PLEASE NOTE: JDK6 is formerly known as Project Mustang