JDK-5038425 : BigInteger doesn't fail on invalid number format (-00-00).
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.math
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2004-04-27
  • Updated: 2004-05-11
  • Resolved: 2004-05-11
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.
Other
5.0 b51Fixed
Related Reports
Relates :  
Description

Name: rmT116609			Date: 04/27/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 :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
If the String passed to the BigInteger(String) constructor is "-00(multiple
0's...)00-00(multiple 0's)00" then the BigInteger object is created even though such a String is badly formatted. When System.out.println (BigInteger) is used for such numbers it throws ArrayIndexOutOfBoundsException.

Normally for these kinds of numbers NumberFormatException should be thrown when the object is created.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the TestCode provided below.
You can give any String with these formats:
" - (one or more 0's) - (one or more 0's) "

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Should throw an NumberFormatException if such numbers are passed to the  BigInteger constructors.
ACTUAL -
1. No Exception when such numbers are passed to the BigInteger constructor.
2. When such numbers are printed with System.out.println() follwing exception is thrown.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
        at java.math.BigInteger.toString(BigInteger.java:2642)
        at java.math.BigInteger.toString(BigInteger.java:2677)
        at java.lang.String.valueOf(String.java:2498)
        at java.io.PrintStream.print(PrintStream.java:558)
        at java.io.PrintStream.println(PrintStream.java:695)
        at TwoSignTest.main(TwoSignTest.java:9)

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
        at java.math.BigInteger.toString(BigInteger.java:2642)
        at java.math.BigInteger.toString(BigInteger.java:2677)
        at java.lang.String.valueOf(String.java:2498)
        at java.io.PrintStream.print(PrintStream.java:558)
        at java.io.PrintStream.println(PrintStream.java:695)
        at TwoSignTest.main(TwoSignTest.java:9)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.math.*;
  public class TwoSignTest
  {
    public static void main(String args[])
    {
      BigInteger b1 = new BigInteger("-000-0");
      // ArrayIndexOutOfBoundsException if System.out.println() used or
     // else no problem.
      System.out.println(b1);
    }
  }
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Check the format before passing it to BigInteger constructor.
(Incident Review ID: 259950) 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger-beta2 FIXED IN: tiger-beta2 INTEGRATED IN: tiger-b51 tiger-beta2
14-06-2004

SUGGESTED FIX src/share/classes/java/math>sccs sccsdiff -r1.67 -r1.68 BigInteger.java ------- BigInteger.java ------- 279c279 < int index = val.indexOf('-'); --- > int index = val.lastIndexOf("-"); ###@###.### 2004-04-29
29-04-2004

EVALUATION Reproduced with latest Tiger builds; similar to 4489146. Will investigate. ###@###.### 2004-04-27
27-04-2004