8272541: Incorrect overflow test in Toom-Cook branch of BigInteger multiplication

Reviewed-by: darcy
This commit is contained in:
Brian Burkhalter 2021-08-27 16:09:28 +00:00
parent dfeb4132e4
commit d1aeca117c
2 changed files with 27 additions and 12 deletions

View File

@ -1670,8 +1670,8 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
// are only considering the magnitudes as non-negative. The // are only considering the magnitudes as non-negative. The
// Toom-Cook multiplication algorithm determines the sign // Toom-Cook multiplication algorithm determines the sign
// at its end from the two signum values. // at its end from the two signum values.
if (bitLength(mag, mag.length) + if ((long)bitLength(mag, mag.length) +
bitLength(val.mag, val.mag.length) > (long)bitLength(val.mag, val.mag.length) >
32L*MAX_MAG_LENGTH) { 32L*MAX_MAG_LENGTH) {
reportOverflow(); reportOverflow();
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,28 +23,43 @@
/* /*
* @test * @test
* @bug 6910473 * @bug 6910473 8272541
* @summary Test that bitLength() is not negative * @summary Test that bitLength() is not negative
* @author Dmitry Nadezhin * @author Dmitry Nadezhin
*/ */
import java.math.BigInteger; import java.math.BigInteger;
import java.util.function.Supplier;
public class BitLengthOverflow { public class BitLengthOverflow {
private static void test(Supplier<BigInteger> s) {
public static void main(String[] args) {
try { try {
BigInteger x = BigInteger.ONE.shiftLeft(Integer.MAX_VALUE); // x = pow(2,Integer.MAX_VALUE) BigInteger x = s.get();
if (x.bitLength() != (1L << 31)) { System.out.println("Surprisingly passed with correct bitLength() " +
throw new RuntimeException("Incorrect bitLength() " + x.bitLength()); x.bitLength());
}
System.out.println("Surprisingly passed with correct bitLength() " + x.bitLength());
} catch (ArithmeticException e) { } catch (ArithmeticException e) {
// expected // expected
System.out.println("Overflow is reported by ArithmeticException, as expected"); System.out.println("Overflow reported by ArithmeticException, as expected");
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {
// possible // possible
System.err.println("BitLengthOverflow skipped: OutOfMemoryError"); System.err.println("BitLengthOverflow skipped: OutOfMemoryError");
System.err.println("Run jtreg with -javaoption:-Xmx8g"); System.err.println("Run jtreg with -javaoption:-Xmx8g");
} }
} }
public static void main(String[] args) {
test(() -> {
// x = pow(2,Integer.MAX_VALUE)
BigInteger x = BigInteger.ONE.shiftLeft(Integer.MAX_VALUE);
if (x.bitLength() != (1L << 31)) {
throw new RuntimeException("Incorrect bitLength() " +
x.bitLength());
}
return x;
});
test(() -> {
BigInteger a = BigInteger.ONE.shiftLeft(1073742825);
BigInteger b = BigInteger.ONE.shiftLeft(1073742825);
return a.multiply(b);
});
}
} }