File tree 2 files changed +24
-3
lines changed
src/java.base/share/classes/java/math
test/jdk/java/math/BigInteger
2 files changed +24
-3
lines changed Original file line number Diff line number Diff line change @@ -3889,7 +3889,9 @@ public boolean isProbablePrime(int certainty) {
3889
3889
return true ;
3890
3890
if (!w .testBit (0 ) || w .equals (ONE ))
3891
3891
return false ;
3892
-
3892
+ if (w .bitLength () > PRIME_SEARCH_BIT_LENGTH_LIMIT + 1 ) {
3893
+ throw new ArithmeticException ("Primality test implementation restriction on bitLength" );
3894
+ }
3893
3895
return w .primeToCertainty (certainty , null );
3894
3896
}
3895
3897
Original file line number Diff line number Diff line change 1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
26
26
* @library /test/lib
27
27
* @build jdk.test.lib.RandomFactory
28
28
* @run main PrimeTest
29
- * @bug 8026236 8074460 8078672
29
+ * @bug 8026236 8074460 8078672 8294593
30
30
* @summary test primality verification methods in BigInteger (use -Dseed=X to set PRNG seed)
31
31
* @author bpb
32
32
* @key randomness
@@ -86,6 +86,10 @@ public static void main(String[] args) throws Exception {
86
86
throw new Exception ("PrimeTest FAILED!" );
87
87
}
88
88
89
+ if (!checkHugeFails ()) {
90
+ throw new Exception ("Primality test on huge integer should fail but succeeded" );
91
+ }
92
+
89
93
System .out .println ("PrimeTest succeeded!" );
90
94
}
91
95
@@ -230,4 +234,19 @@ private static boolean checkMersennePrimes(int certainty) {
230
234
231
235
return result ;
232
236
}
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
+
233
252
}
You can’t perform that action at this time.
0 commit comments