RFR: 6471906 - NegativeArraySizeException in tenToThe()
Joe Darcy
joe.darcy at oracle.com
Wed Jan 30 18:58:16 UTC 2013
Hi Brian,
Looks fine. I think not have a regression test for this is okay since
it would take a relatively long time to run and use a significant amount
of memory.
Thanks,
-Joe
On 01/30/2013 10:34 AM, Brian Burkhalter wrote:
> Please review at your convenience.
>
> Issue: JDK-6471906
>
> Assessment:
>
> The problem occurs because an attempt is made to allocate an array of length > Integer.MAX_VALUE.
>
> Proposed solution:
>
> --- a/src/share/classes/java/math/BigDecimal.java Fri Jan 25 12:25:10 2013 -0800
> +++ b/src/share/classes/java/math/BigDecimal.java Tue Jan 29 13:23:47 2013 -0800
> @@ -3537,13 +3537,25 @@
> else
> return expandBigIntegerTenPowers(n);
> }
> - // BigInteger.pow is slow, so make 10**n by constructing a
> - // BigInteger from a character string (still not very fast)
> - char tenpow[] = new char[n + 1];
> - tenpow[0] = '1';
> - for (int i = 1; i <= n; i++)
> - tenpow[i] = '0';
> - return new BigInteger(tenpow,1, tenpow.length);
> +
> + if (n < 1024*524288) {
> + // BigInteger.pow is slow, so make 10**n by constructing a
> + // BigInteger from a character string (still not very fast)
> + // which occupies no more than 1GB (!) of memory.
> + char tenpow[] = new char[n + 1];
> + tenpow[0] = '1';
> + for (int i = 1; i <= n; i++) {
> + tenpow[i] = '0';
> + }
> + return new BigInteger(tenpow, 1, tenpow.length);
> + }
> +
> + if ((n & 0x1) == 0x1) {
> + return BigInteger.TEN.multiply(bigTenToThe(n - 1));
> + } else {
> + BigInteger tmp = bigTenToThe(n/2);
> + return tmp.multiply(tmp);
> + }
> }
>
> The portion of the method using the tenpow array is retained for performance (speed of execution) reasons as the BigInteger multiplications for large values are slow. It is intended to address this latter problem separately.
>
> Thanks,
>
> Brian
More information about the core-libs-dev
mailing list