Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8042736

BitSet with Integer.MAX_VALUE as index throws IndexOutOfBoundsException

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 8
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0"
      Java(TM) SE Runtime Environment (build 1.8.0-b132)
      Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Darwin Kernel Version 12.5.0

      A DESCRIPTION OF THE PROBLEM :
      BitSet with Integer.MAX_VALUE throws exception during toString call.

      Example:

      BitSet data = new BitSet();
      data.set(Integer.MAX_VALUE);
      System.out.println(data);

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      BitSet data = new BitSet();
      data.set(Integer.MAX_VALUE);
      System.out.println(data);

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      {2147483647}
      ACTUAL -
      java.lang.IndexOutOfBoundsException: fromIndex < 0: -2147483648
      at java.util.BitSet.nextSetBit(BitSet.java:709)
      at java.util.BitSet.toString(BitSet.java:1189)
      at java.lang.String.valueOf(String.java:2979)
      at java.io.PrintStream.println(PrintStream.java:821)
      at com.max.algs.AlgorithmsMain.<init>(AlgorithmsMain.java:132)
      at com.max.algs.AlgorithmsMain.main(AlgorithmsMain.java:141)

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.lang.IndexOutOfBoundsException: fromIndex < 0: -2147483648
      at java.util.BitSet.nextSetBit(BitSet.java:709)
      at java.util.BitSet.toString(BitSet.java:1189)
      at java.lang.String.valueOf(String.java:2979)
      at java.io.PrintStream.println(PrintStream.java:821)
      at com.max.algs.AlgorithmsMain.<init>(AlgorithmsMain.java:132)
      at com.max.algs.AlgorithmsMain.main(AlgorithmsMain.java:141)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      CUSTOMER SUBMITTED WORKAROUND :
      public String toString() {
              checkInvariants();

              int numBits = (wordsInUse > 128) ?
                  cardinality() : wordsInUse * BITS_PER_WORD;
              StringBuilder b = new StringBuilder(6*numBits + 2);
              b.append('{');

              int i = nextSetBit(0);
              
              if (i != -1) {
                  b.append(i);
                  
                  if( i != Integer.MAX_VALUE ){
                            
      for (i = nextSetBit(i+1); i >= 0 && i != Integer.MAX_VALUE; i = nextSetBit(i+1)) {
      b.append(", ").append(i);
      }

      if( i == Integer.MAX_VALUE ){
      b.append(", ").append(i);
      }
                  }
              }

              b.append('}');
              return b.toString();
          }

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: