Skip to content

Commit 081691a

Browse files
committed
8294593: Check the size of the target on invocations of BigInteger::isProbablePrime
Reviewed-by: darcy
1 parent a4f2078 commit 081691a

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

Diff for: src/java.base/share/classes/java/math/BigInteger.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -3889,7 +3889,9 @@ public boolean isProbablePrime(int certainty) {
38893889
return true;
38903890
if (!w.testBit(0) || w.equals(ONE))
38913891
return false;
3892-
3892+
if (w.bitLength() > PRIME_SEARCH_BIT_LENGTH_LIMIT + 1) {
3893+
throw new ArithmeticException("Primality test implementation restriction on bitLength");
3894+
}
38933895
return w.primeToCertainty(certainty, null);
38943896
}
38953897

Diff for: test/jdk/java/math/BigInteger/PrimeTest.java

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2022, 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
@@ -26,7 +26,7 @@
2626
* @library /test/lib
2727
* @build jdk.test.lib.RandomFactory
2828
* @run main PrimeTest
29-
* @bug 8026236 8074460 8078672
29+
* @bug 8026236 8074460 8078672 8294593
3030
* @summary test primality verification methods in BigInteger (use -Dseed=X to set PRNG seed)
3131
* @author bpb
3232
* @key randomness
@@ -86,6 +86,10 @@ public static void main(String[] args) throws Exception {
8686
throw new Exception("PrimeTest FAILED!");
8787
}
8888

89+
if (!checkHugeFails()) {
90+
throw new Exception("Primality test on huge integer should fail but succeeded");
91+
}
92+
8993
System.out.println("PrimeTest succeeded!");
9094
}
9195

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

231235
return result;
232236
}
237+
238+
private static boolean checkHugeFails() {
239+
try {
240+
// huge odd integer
241+
BigInteger a = BigInteger.ONE.shiftLeft(500_000_000 + 1)
242+
.setBit(0);
243+
a.isProbablePrime(1);
244+
// not expected to reach here
245+
return false;
246+
} catch (ArithmeticException e) {
247+
// this is the expected behavior
248+
return true;
249+
}
250+
}
251+
233252
}

0 commit comments

Comments
 (0)