JDK-6819773 : BigDecimal.divide(BigDecimal,int,int) throws ArrayIndexOutofBoundsException under some circumstances
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.math
  • Affected Version: 6u14
  • Priority: P2
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2009-03-19
  • Updated: 2011-02-15
  • Resolved: 2009-03-28
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
6u14 b04Fixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Description
java.math.BigDecimal.divide(BigDecimal divisor, int scale, int roundingMode) 
throws unspecified ArrayIndexOutOfBoundsException
when roundingMode equals BigDecimal.ROUND_HALF_EVEN


How to reproduce:
-----------------

To reproduce this bug run the following BigDecimalTest

NOTE: the code is copied from Apache license Harmony test.

-------------------BigDecimalTest.java--------
import java.math.*;

import java.math.*;

public class BigDecimalTest {
    public static void main(String[] args) {
        String dec1 = "-37361671119238118911893939591735";
        int scale1 = 10;
        String dec2 = "74723342238476237823787879183470";
        int scale2 = 15;
        int resScale = -5;
        BigDecimal decN1 = new BigDecimal(new BigInteger(dec1), scale1);
        BigDecimal decN2 = new BigDecimal(new BigInteger(dec2), scale2);
        BigDecimal result;
        int round = -1;
        String sRound = "";
        try {
            round = BigDecimal.ROUND_HALF_UP;
            sRound = "BigDecimal.ROUND_HALF_UP";
            result = decN1.divide(decN2, resScale, round);
            System.out.println("divide with " + sRound + " passsed. result="
                + result);

            round = BigDecimal.ROUND_HALF_DOWN;
            sRound = "BigDecimal.ROUND_HALF_DOWN";
            result = decN1.divide(decN2, resScale, round);
            System.out.println("divide with " + sRound + " passsed. result="
                + result);

            round = BigDecimal.ROUND_HALF_EVEN;
            sRound = "BigDecimal.ROUND_HALF_EVEN";
            result = decN1.divide(decN2, resScale, round);
            System.out.println("divide with " + sRound + " passsed. result="
                + result);

        } catch (Throwable e) {
            System.out.println("divide with " + sRound + " failed");
            System.out.println("Unexpected exception was thrown. " + round);
            e.printStackTrace(System.out);
        }
    }
}
------------

Diagnostics/output:
-------------------

Output on 6u14 b03 ia32:

java version "1.6.0_14-ea"
Java(TM) SE Runtime Environment (build 1.6.0_14-ea-b03)
Java HotSpot(TM) Client VM (build 14.0-b12, mixed mode, sharing)

divide with BigDecimal.ROUND_HALF_UP passsed. result=-1E+5
divide with BigDecimal.ROUND_HALF_DOWN passsed. result=0E+5
divide with BigDecimal.ROUND_HALF_EVEN failed
Unexpected exception was thrown. 6
java.lang.ArrayIndexOutOfBoundsException: -1
        at java.math.MutableBigInteger.isOdd(Unknown Source)
        at java.math.BigDecimal.divideAndRound(Unknown Source)
        at java.math.BigDecimal.divide(Unknown Source)
        at BigDecimalTest.main(BigDecimalTest.java:30)

Comments
EVALUATION See comments section.
19-03-2009