JDK-6590391 : BigInteger.ONE.shiftLeft(Integer.MIN_VALUE) Recurses Infinitely
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.math
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2007-08-08
  • Updated: 2010-04-04
  • Resolved: 2007-08-08
Related Reports
Duplicate :  
Description
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 ----------

Comments
EVALUATION Closing as duplicate of 6371401.
08-08-2007