Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8294593: Check the size of the target on invocations of BigInteger::i…
…sProbablePrime

Reviewed-by: darcy
  • Loading branch information
rgiulietti committed Oct 3, 2022
1 parent a4f2078 commit 081691a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/java.base/share/classes/java/math/BigInteger.java
Expand Up @@ -3889,7 +3889,9 @@ public boolean isProbablePrime(int certainty) {
return true;
if (!w.testBit(0) || w.equals(ONE))
return false;

if (w.bitLength() > PRIME_SEARCH_BIT_LENGTH_LIMIT + 1) {
throw new ArithmeticException("Primality test implementation restriction on bitLength");
}
return w.primeToCertainty(certainty, null);
}

Expand Down
23 changes: 21 additions & 2 deletions test/jdk/java/math/BigInteger/PrimeTest.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2022, 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 @@ -26,7 +26,7 @@
* @library /test/lib
* @build jdk.test.lib.RandomFactory
* @run main PrimeTest
* @bug 8026236 8074460 8078672
* @bug 8026236 8074460 8078672 8294593
* @summary test primality verification methods in BigInteger (use -Dseed=X to set PRNG seed)
* @author bpb
* @key randomness
Expand Down Expand Up @@ -86,6 +86,10 @@ public static void main(String[] args) throws Exception {
throw new Exception("PrimeTest FAILED!");
}

if (!checkHugeFails()) {
throw new Exception("Primality test on huge integer should fail but succeeded");
}

System.out.println("PrimeTest succeeded!");
}

Expand Down Expand Up @@ -230,4 +234,19 @@ private static boolean checkMersennePrimes(int certainty) {

return result;
}

private static boolean checkHugeFails() {
try {
// huge odd integer
BigInteger a = BigInteger.ONE.shiftLeft(500_000_000 + 1)
.setBit(0);
a.isProbablePrime(1);
// not expected to reach here
return false;
} catch (ArithmeticException e) {
// this is the expected behavior
return true;
}
}

}

0 comments on commit 081691a

Please sign in to comment.