Skip to content

Commit 2678bab

Browse files
committed
Negative values are always considered not prime.
Fixes jruby/jruby#4193. Fixes #107.
1 parent 4458259 commit 2678bab

File tree

1 file changed

+12
-2
lines changed
  • src/main/java/org/jruby/ext/openssl

1 file changed

+12
-2
lines changed

src/main/java/org/jruby/ext/openssl/BN.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,9 +623,14 @@ public RubyFixnum num_bits_set(final ThreadContext context) {
623623
public IRubyObject prime_p(IRubyObject[] args) {
624624
final Ruby runtime = getRuntime();
625625
int argc = Arity.checkArgumentCount(runtime, args, 0, 1);
626+
627+
// negative numbers are always considered non-prime
628+
if (this.value.signum() < 0) return runtime.getFalse();
629+
630+
int certainty = argc == 0 ? DEFAULT_CERTAINTY : RubyNumeric.fix2int(args[0]);
631+
626632
// BigInteger#isProbablePrime will actually limit checks to a maximum of 50,
627633
// depending on bit count.
628-
int certainty = argc == 0 ? DEFAULT_CERTAINTY : RubyNumeric.fix2int(args[0]);
629634
return runtime.newBoolean(this.value.isProbablePrime(certainty));
630635
}
631636

@@ -635,9 +640,14 @@ public IRubyObject prime_p(IRubyObject[] args) {
635640
public IRubyObject prime_fasttest_p(IRubyObject[] args) {
636641
final Ruby runtime = getRuntime();
637642
int argc = Arity.checkArgumentCount(runtime, args, 0, 2);
643+
644+
// negative numbers are always considered non-prime
645+
if (this.value.signum() < 0) return runtime.getFalse();
646+
647+
int certainty = argc == 0 ? DEFAULT_CERTAINTY : RubyNumeric.fix2int(args[0]);
648+
638649
// BigInteger#isProbablePrime will actually limit checks to a maximum of 50,
639650
// depending on bit count.
640-
int certainty = argc == 0 ? DEFAULT_CERTAINTY : RubyNumeric.fix2int(args[0]);
641651
return runtime.newBoolean(this.value.isProbablePrime(certainty));
642652
}
643653

0 commit comments

Comments
 (0)