Skip to content

Explicit check for true instead of true-ish in OpenSSL::BN.rand #1116

Description

@herwinw

This method has the following call sequence:

BN.rand(bits [, fill [, odd]]) -> aBN

If we look at the code at

bottom = (odd == Qtrue) ? 1 : 0;

we see an explicit check for == Qtrue. This means passing any other value that is true-ish in Ruby is interpreted as false:

$ ruby -ropenssl -e 'p OpenSSL::BN.rand(8, 0, 1).to_i'
140
$ ruby -ropenssl -e 'p OpenSSL::BN.rand(8, 0, 1).to_i'
158

Here the first line generated an even number, even though we pass in a true-ish value.

bottom = RTEST(odd) ? 1 : 0;

I think this should be the preferred way.

If this is intentional (or should be kept to preserve behaviour), it should probably be reflected in the docs.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions