JDK-8040806 : BitSet.toString() can throw IndexOutOfBoundsException
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 7u51
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2014-04-16
  • Updated: 2015-01-21
  • Resolved: 2014-05-06
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 8 JDK 9
8u40Fixed 9 b13Fixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :


A DESCRIPTION OF THE PROBLEM :
When set Integer.MAX_VALUE - 1 to BitSet, then call the toString method.
It will throw exception: java.lang.IndexOutOfBoundsException: fromIndex < 0: -2147483648

Test code:
    public static void main(String[] args) {
        BitSet bitSet = new BitSet();
        bitSet.set(Integer.MAX_VALUE - 1);
        System.out.println( bitSet.toString());
    }

The bug was occured at 1189 line of BitSet source.
for (i = nextSetBit(i+1); i >= 0; i = nextSetBit(i+1)) {

If set Integer.MAX_VALUE to BitSet, exception will throw at the first nextSetBit(i+1). If set Integer.MAX_VALUE - 1 to BitSet, exception will throw at the second nextSetBit(i+1). 

I think before call nextSetBit(i+1) method, to check i is equal to Integer.MAX_VALUE is better.




REPRODUCIBILITY :
This bug can be reproduced always.