JDK-8015832 : BigDecimal throws an ArrayIndexOutOfBoundsException if string ends in 'E'
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.math
  • Affected Version: 7
  • Priority: P3
  • Status: Resolved
  • Resolution: Cannot Reproduce
  • Submitted: 2013-05-19
  • Updated: 2013-06-10
  • Resolved: 2013-06-08
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :


A DESCRIPTION OF THE PROBLEM :
It assumes that there will always be an exponent following an E.  If there isn't, this throws an AIOOBE:

                // exponent expected
                if ((c != 'e') && (c != 'E'))
                    throw new NumberFormatException();
                offset++;
                c = in[offset]; <---- exception

Parsing this invalid string should presumably throw a NumberFormatException, instead.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
new BigDecimal( " 1.234E " );


REPRODUCIBILITY :
This bug can be reproduced always.
Comments
This problem is already covered by JTREG in test/java/math/BigDecimal/StringConstructor.java. If desirable however, this test could be updated to include the exact case given in this issue description: --- a/test/java/math/BigDecimal/StringConstructor.java Mon Jun 10 11:06:26 2013 -0700 +++ b/test/java/math/BigDecimal/StringConstructor.java Mon Jun 10 12:51:28 2013 -0700 @@ -58,6 +58,7 @@ constructWithError("11.e-+"); constructWithError("11.e-+1"); constructWithError("11.e+-1"); + constructWithError("1.234E"); // Range checks constructWithError("1e"+Integer.MIN_VALUE);
10-06-2013

The fix for JDK-7082971 is not in the Hg history of BigDecimal at http://hg.openjdk.java.net/jdk7u/jdk7u-dev/jdk, if this is the place to look for a backport.
08-06-2013

Not reproducible on Mac OS X in 7u21 or 8-dev.
08-06-2013

Looks like this problem was fixed as a side-effect of JDK-7082971. Please verify and check for a regression test of the exponent case.
07-06-2013

This appears to have been corrected since whichever Java 7 update was used to reproduce the problem which prompted this bug report. Using 7u21 on Mac OS X one obtains Exception in thread "main" java.lang.NumberFormatException at java.math.BigDecimal.<init>(BigDecimal.java:545) at java.math.BigDecimal.<init>(BigDecimal.java:739) where the pertinent BigDecimal source is 544 } catch (ArrayIndexOutOfBoundsException e) { 545 throw new NumberFormatException();
07-06-2013

In Java 8 a NumberFormatException is indeed thrown in this case, as would be more apropos.
07-06-2013