diff options
author | Doug Hogan <[email protected]> | 2019-01-03 19:15:46 -0800 |
---|---|---|
committer | Doug Hogan <[email protected]> | 2019-01-08 00:08:22 -0800 |
commit | ce0d975cde2d67c8e5dd3fa024559f4d0a2df7ad (patch) | |
tree | 4e947dbf1b5ab300dd3ef2ebc316f94be74702f2 | |
parent | 536f1fff1596de0c1591e9c796d0ab8b6caf21e7 (diff) | |
download | otp-ce0d975cde2d67c8e5dd3fa024559f4d0a2df7ad.tar.gz otp-ce0d975cde2d67c8e5dd3fa024559f4d0a2df7ad.tar.bz2 otp-ce0d975cde2d67c8e5dd3fa024559f4d0a2df7ad.zip |
Revamp strong_rand_range_nif()
* Add error checking for all OpenSSL calls.
-rw-r--r-- | lib/crypto/c_src/rand.c | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/lib/crypto/c_src/rand.c b/lib/crypto/c_src/rand.c index 37d4490fc4..4a8be4c55f 100644 --- a/lib/crypto/c_src/rand.c +++ b/lib/crypto/c_src/rand.c @@ -51,22 +51,33 @@ ERL_NIF_TERM strong_rand_bytes_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM ERL_NIF_TERM strong_rand_range_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {/* (Range) */ - BIGNUM *bn_range, *bn_rand; + BIGNUM *bn_range = NULL, *bn_rand = NULL; ERL_NIF_TERM ret; - if(!get_bn_from_bin(env, argv[0], &bn_range)) { - return enif_make_badarg(env); - } + if (argc != 1) + goto bad_arg; + if (!get_bn_from_bin(env, argv[0], &bn_range)) + goto bad_arg; - bn_rand = BN_new(); - if (BN_rand_range(bn_rand, bn_range) != 1) { - ret = atom_false; - } - else { - ret = bin_from_bn(env, bn_rand); - } - BN_free(bn_rand); - BN_free(bn_range); + if ((bn_rand = BN_new()) == NULL) + goto err; + if (!BN_rand_range(bn_rand, bn_range)) + goto err; + + ret = bin_from_bn(env, bn_rand); + goto done; + + bad_arg: + return enif_make_badarg(env); + + err: + ret = atom_false; + + done: + if (bn_rand) + BN_free(bn_rand); + if (bn_range) + BN_free(bn_range); return ret; } |