Skip to content

Commit

Permalink
8272541: Incorrect overflow test in Toom-Cook branch of BigInteger mu…
Browse files Browse the repository at this point in the history
…ltiplication

Reviewed-by: darcy
  • Loading branch information
Brian Burkhalter committed Aug 27, 2021
1 parent dfeb413 commit d1aeca1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/math/BigInteger.java
Original file line number Diff line number Diff line change
Expand Up @@ -1670,8 +1670,8 @@ private BigInteger multiply(BigInteger val, boolean isRecursion) {
// are only considering the magnitudes as non-negative. The
// Toom-Cook multiplication algorithm determines the sign
// at its end from the two signum values.
if (bitLength(mag, mag.length) +
bitLength(val.mag, val.mag.length) >
if ((long)bitLength(mag, mag.length) +
(long)bitLength(val.mag, val.mag.length) >
32L*MAX_MAG_LENGTH) {
reportOverflow();
}
Expand Down
35 changes: 25 additions & 10 deletions test/jdk/java/math/BigInteger/BitLengthOverflow.java
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,28 +23,43 @@

/*
* @test
* @bug 6910473
* @bug 6910473 8272541
* @summary Test that bitLength() is not negative
* @author Dmitry Nadezhin
*/
import java.math.BigInteger;
import java.util.function.Supplier;

public class BitLengthOverflow {

public static void main(String[] args) {
private static void test(Supplier<BigInteger> s) {
try {
BigInteger x = BigInteger.ONE.shiftLeft(Integer.MAX_VALUE); // x = pow(2,Integer.MAX_VALUE)
if (x.bitLength() != (1L << 31)) {
throw new RuntimeException("Incorrect bitLength() " + x.bitLength());
}
System.out.println("Surprisingly passed with correct bitLength() " + x.bitLength());
BigInteger x = s.get();
System.out.println("Surprisingly passed with correct bitLength() " +
x.bitLength());
} catch (ArithmeticException e) {
// expected
System.out.println("Overflow is reported by ArithmeticException, as expected");
System.out.println("Overflow reported by ArithmeticException, as expected");
} catch (OutOfMemoryError e) {
// possible
System.err.println("BitLengthOverflow skipped: OutOfMemoryError");
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);
});
}
}

9 comments on commit d1aeca1

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gnu-andrew
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on d1aeca1 Feb 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gnu-andrew the backport was successfully created on the branch gnu-andrew-backport-d1aeca11 in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit d1aeca11 from the openjdk/jdk repository.

The commit being backported was authored by Brian Burkhalter on 27 Aug 2021 and was reviewed by Joe Darcy.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev gnu-andrew-backport-d1aeca11:gnu-andrew-backport-d1aeca11
$ git checkout gnu-andrew-backport-d1aeca11
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev gnu-andrew-backport-d1aeca11

@gnu-andrew
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk11u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on d1aeca1 Feb 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gnu-andrew the backport was successfully created on the branch gnu-andrew-backport-d1aeca11 in my personal fork of openjdk/jdk11u-dev. To create a pull request with this backport targeting openjdk/jdk11u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit d1aeca11 from the openjdk/jdk repository.

The commit being backported was authored by Brian Burkhalter on 27 Aug 2021 and was reviewed by Joe Darcy.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk11u-dev:

$ git fetch https://github.com/openjdk-bots/jdk11u-dev gnu-andrew-backport-d1aeca11:gnu-andrew-backport-d1aeca11
$ git checkout gnu-andrew-backport-d1aeca11
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk11u-dev gnu-andrew-backport-d1aeca11

@gnu-andrew
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk13u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on d1aeca1 Feb 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gnu-andrew the backport was successfully created on the branch gnu-andrew-backport-d1aeca11 in my personal fork of openjdk/jdk13u-dev. To create a pull request with this backport targeting openjdk/jdk13u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit d1aeca11 from the openjdk/jdk repository.

The commit being backported was authored by Brian Burkhalter on 27 Aug 2021 and was reviewed by Joe Darcy.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk13u-dev:

$ git fetch https://github.com/openjdk-bots/jdk13u-dev gnu-andrew-backport-d1aeca11:gnu-andrew-backport-d1aeca11
$ git checkout gnu-andrew-backport-d1aeca11
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk13u-dev gnu-andrew-backport-d1aeca11

@gnu-andrew
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk15u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on d1aeca1 Feb 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gnu-andrew the backport was successfully created on the branch gnu-andrew-backport-d1aeca11 in my personal fork of openjdk/jdk15u-dev. To create a pull request with this backport targeting openjdk/jdk15u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit d1aeca11 from the openjdk/jdk repository.

The commit being backported was authored by Brian Burkhalter on 27 Aug 2021 and was reviewed by Joe Darcy.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk15u-dev:

$ git fetch https://github.com/openjdk-bots/jdk15u-dev gnu-andrew-backport-d1aeca11:gnu-andrew-backport-d1aeca11
$ git checkout gnu-andrew-backport-d1aeca11
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk15u-dev gnu-andrew-backport-d1aeca11

Please sign in to comment.