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

BigInteger.ONE.shiftLeft(Integer.MIN_VALUE) Recurses Infinitely

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 6
    • core-libs

      FULL PRODUCT 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)


      ADDITIONAL OS VERSION INFORMATION :
      Linux glimmer 2.6.20-16-generic #2 SMP Thu Jun 7 20:19:32 UTC 2007 i686 GNU/Linux


      A DESCRIPTION OF THE PROBLEM :
      BigInteger.shiftLeft (and .shiftRight, I presume) will cause a stack overflow due to infinite recursion when passed Integer.MIN_VALUE as the shift amount. Most likely, .shiftLeft has code like:

        if(n<0) return shiftRight(-n);

      but, it should be more like:

        if(-n>0) return shiftRight(-n);

      with some further modifications to ensure that having a negative value further down is not a problem (though in the source that I have, this one change would be sufficient to avoid the bug).

      A more thorough fix, which actually returns the correct result, would have to do something more severe.

      [Note: the previous web page did not seem to have a "Subcategory" selection of classes_math.]

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and execute the following t.java:

      import java.math.BigInteger;

      public class t
      {
              public static void main(String [] args)
              {
                      System.out.println(BigInteger.ONE.shiftLeft(Integer.MIN_VALUE));
              }
      }


      ACTUAL -
      Exception in thread "main" java.lang.StackOverflowError
              at java.math.BigInteger.shiftLeft(BigInteger.java:2009)
              at java.math.BigInteger.shiftRight(BigInteger.java:2058)
              at java.math.BigInteger.shiftLeft(BigInteger.java:2014)
              at java.math.BigInteger.shiftRight(BigInteger.java:2058)
              at java.math.BigInteger.shiftLeft(BigInteger.java:2014)
              at java.math.BigInteger.shiftRight(BigInteger.java:2058)
              at java.math.BigInteger.shiftLeft(BigInteger.java:2014)
              at java.math.BigInteger.shiftRight(BigInteger.java:2058)
              at java.math.BigInteger.shiftLeft(BigInteger.java:2014)
              at java.math.BigInteger.shiftRight(BigInteger.java:2058)
              at java.math.BigInteger.shiftLeft(BigInteger.java:2014)
              at java.math.BigInteger.shiftRight(BigInteger.java:2058)
              ...

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.StackOverflowError
              at java.math.BigInteger.shiftLeft(BigInteger.java:2009)
              at java.math.BigInteger.shiftRight(BigInteger.java:2058)
              at java.math.BigInteger.shiftLeft(BigInteger.java:2014)
              at java.math.BigInteger.shiftRight(BigInteger.java:2058)
              at java.math.BigInteger.shiftLeft(BigInteger.java:2014)
              at java.math.BigInteger.shiftRight(BigInteger.java:2058)
              at java.math.BigInteger.shiftLeft(BigInteger.java:2014)
              at java.math.BigInteger.shiftRight(BigInteger.java:2058)
              at java.math.BigInteger.shiftLeft(BigInteger.java:2014)
              at java.math.BigInteger.shiftRight(BigInteger.java:2058)
              at java.math.BigInteger.shiftLeft(BigInteger.java:2014)
              at java.math.BigInteger.shiftRight(BigInteger.java:2058)
              ...

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      import java.math.BigInteger;

      public class t
      {
              public static void main(String [] args)
              {
                      System.out.println(BigInteger.ONE.shiftLeft(Integer.MIN_VALUE));
              }
      }

      ---------- END SOURCE ----------

            darcy Joe Darcy
            ryeung Roger Yeung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: