Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 359504c

Browse files
committed
8272541: Incorrect overflow test in Toom-Cook branch of BigInteger multiplication
Backport-of: d1aeca117ccc4334d67b2692ff087a9f8d808a59
1 parent 279be06 commit 359504c

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

src/java.base/share/classes/java/math/BigInteger.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,8 +1653,8 @@ private BigInteger multiply(BigInteger val, boolean isRecursion) {
16531653
// are only considering the magnitudes as non-negative. The
16541654
// Toom-Cook multiplication algorithm determines the sign
16551655
// at its end from the two signum values.
1656-
if (bitLength(mag, mag.length) +
1657-
bitLength(val.mag, val.mag.length) >
1656+
if ((long)bitLength(mag, mag.length) +
1657+
(long)bitLength(val.mag, val.mag.length) >
16581658
32L*MAX_MAG_LENGTH) {
16591659
reportOverflow();
16601660
}

test/jdk/java/math/BigInteger/BitLengthOverflow.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,28 +23,43 @@
2323

2424
/*
2525
* @test
26-
* @bug 6910473
26+
* @bug 6910473 8272541
2727
* @summary Test that bitLength() is not negative
2828
* @author Dmitry Nadezhin
2929
*/
3030
import java.math.BigInteger;
31+
import java.util.function.Supplier;
3132

3233
public class BitLengthOverflow {
33-
34-
public static void main(String[] args) {
34+
private static void test(Supplier<BigInteger> s) {
3535
try {
36-
BigInteger x = BigInteger.ONE.shiftLeft(Integer.MAX_VALUE); // x = pow(2,Integer.MAX_VALUE)
37-
if (x.bitLength() != (1L << 31)) {
38-
throw new RuntimeException("Incorrect bitLength() " + x.bitLength());
39-
}
40-
System.out.println("Surprisingly passed with correct bitLength() " + x.bitLength());
36+
BigInteger x = s.get();
37+
System.out.println("Surprisingly passed with correct bitLength() " +
38+
x.bitLength());
4139
} catch (ArithmeticException e) {
4240
// expected
43-
System.out.println("Overflow is reported by ArithmeticException, as expected");
41+
System.out.println("Overflow reported by ArithmeticException, as expected");
4442
} catch (OutOfMemoryError e) {
4543
// possible
4644
System.err.println("BitLengthOverflow skipped: OutOfMemoryError");
4745
System.err.println("Run jtreg with -javaoption:-Xmx8g");
4846
}
4947
}
48+
49+
public static void main(String[] args) {
50+
test(() -> {
51+
// x = pow(2,Integer.MAX_VALUE)
52+
BigInteger x = BigInteger.ONE.shiftLeft(Integer.MAX_VALUE);
53+
if (x.bitLength() != (1L << 31)) {
54+
throw new RuntimeException("Incorrect bitLength() " +
55+
x.bitLength());
56+
}
57+
return x;
58+
});
59+
test(() -> {
60+
BigInteger a = BigInteger.ONE.shiftLeft(1073742825);
61+
BigInteger b = BigInteger.ONE.shiftLeft(1073742825);
62+
return a.multiply(b);
63+
});
64+
}
5065
}

0 commit comments

Comments
 (0)