United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-5050278 : BigInteger constructor gives NegativeArraySizeException

Details
Type:
Bug
Submit Date:
2004-05-20
Status:
Resolved
Updated Date:
2004-11-06
Project Name:
JDK
Resolved Date:
2004-11-06
Component:
core-libs
OS:
linux
Sub-Component:
java.math
CPU:
x86
Priority:
P4
Resolution:
Fixed
Affected Versions:
1.4.2
Fixed Versions:

Related Reports
Relates:
Relates:

Sub Tasks

Description
Name: rmT116609			Date: 05/19/2004


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

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 escher.nz.reeltwo.com 2.4.22-1.2140.nptl #1 Tue Jan 6 20:20:43 EST 2004 i686 i686 i386 GNU/Linux
Solaris 2.9


A DESCRIPTION OF THE PROBLEM :
The constructor BigInteger(int numBits, Random rnd)  fails with NegativeArraySizeException when numBits is Integer.MAX_VALUE.  What it should do (according to the docs) is produce a BigInteger with 2^31-1 bits (i.e. a very large number).

In fact sizes Integer.MAX_VALUE-1,  Integer.MAX_VALUE-2,  down to Integer.MAX_VALUE-6 also cause this problem. Integer.MAX_VALUE-7 works as expected.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Calling the constructor with the indicated arguments causes the problem.
The sample code below demonstrates the problem.



EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I expect a random BigInteger with the specified number of bits to be constructed.
ACTUAL -
Exception.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
escher[sean]:src> !ja
java -Xmx800m Bug
Exception in thread "main" java.lang.NegativeArraySizeException
        at java.math.BigInteger.randomBits(BigInteger.java:471)
        at java.math.BigInteger.<init>(BigInteger.java:464)
        at Bug.main(Bug.java:7)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.Random;
import java.math.BigInteger;

public class Bug {

  public static void main(String[] args) {
    BigInteger a = new BigInteger(Integer.MAX_VALUE, new Random());
  }
}

---------- END SOURCE ----------
(Incident Review ID: 270492) 
======================================================================
###@###.### 10/21/04 01:45 GMT

                                    

Comments
SUGGESTED FIX

src/share/classes/java/math>sccs sccsdiff -r1.68 -r1.69 BigInteger.java

------- BigInteger.java -------
470c470
<       int numBytes = (numBits+7)/8;
---
>       int numBytes = (int)(((long)numBits+7)/8); // avoid overflow
###@###.### 10/26/04 21:48 GMT
                                     
2004-10-26
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
dragon


                                     
2004-07-01
EVALUATION

Yes; there are some minor integer overflow issues in private methods called by that constructor.

###@###.### 2004-05-20
                                     
2004-05-20



Hardware and Software, Engineered to Work Together