JDK-5050278 : BigInteger constructor gives NegativeArraySizeException
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.math
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2004-05-20
  • Updated: 2004-11-06
  • Resolved: 2004-11-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 6
6 b12Fixed
Related Reports
Relates :  
Relates :  
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
26-10-2004

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: dragon
01-07-2004

EVALUATION Yes; there are some minor integer overflow issues in private methods called by that constructor. ###@###.### 2004-05-20
20-05-2004