aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crypto/c_src
diff options
context:
space:
mode:
authorDoug Hogan <[email protected]>2019-01-03 22:18:09 -0800
committerDoug Hogan <[email protected]>2019-01-08 01:11:57 -0800
commita23b898f54b9601872c130ccfc7b2a86f6124e4f (patch)
treeb581e2c22c3da186624dda408bec6e37c5af1a65 /lib/crypto/c_src
parent2e31e2cdcab0de8e00e4b3103a92440d81541211 (diff)
downloadotp-a23b898f54b9601872c130ccfc7b2a86f6124e4f.tar.gz
otp-a23b898f54b9601872c130ccfc7b2a86f6124e4f.tar.bz2
otp-a23b898f54b9601872c130ccfc7b2a86f6124e4f.zip
Revamp hash_update_nif()
* Add error handling for all OpenSSL and Erlang calls.
Diffstat (limited to 'lib/crypto/c_src')
-rw-r--r--lib/crypto/c_src/hash.c38
1 files changed, 25 insertions, 13 deletions
diff --git a/lib/crypto/c_src/hash.c b/lib/crypto/c_src/hash.c
index a2c53bcaaf..482a3e1a12 100644
--- a/lib/crypto/c_src/hash.c
+++ b/lib/crypto/c_src/hash.c
@@ -134,26 +134,38 @@ ERL_NIF_TERM hash_init_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
ERL_NIF_TERM hash_update_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{/* (Context, Data) */
- struct evp_md_ctx *ctx, *new_ctx;
+ struct evp_md_ctx *ctx, *new_ctx = NULL;
ErlNifBinary data;
ERL_NIF_TERM ret;
- if (!enif_get_resource(env, argv[0], evp_md_ctx_rtype, (void**)&ctx) ||
- !enif_inspect_iolist_as_binary(env, argv[1], &data)) {
- return enif_make_badarg(env);
- }
+ if (argc != 2)
+ goto bad_arg;
+ if (!enif_get_resource(env, argv[0], evp_md_ctx_rtype, (void**)&ctx))
+ goto bad_arg;
+ if (!enif_inspect_iolist_as_binary(env, argv[1], &data))
+ goto bad_arg;
- new_ctx = enif_alloc_resource(evp_md_ctx_rtype, sizeof(struct evp_md_ctx));
- new_ctx->ctx = EVP_MD_CTX_new();
- if (!EVP_MD_CTX_copy(new_ctx->ctx, ctx->ctx) ||
- !EVP_DigestUpdate(new_ctx->ctx, data.data, data.size)) {
- enif_release_resource(new_ctx);
- return atom_notsup;
- }
+ if ((new_ctx = enif_alloc_resource(evp_md_ctx_rtype, sizeof(struct evp_md_ctx))) == NULL)
+ goto err;
+ if ((new_ctx->ctx = EVP_MD_CTX_new()) == NULL)
+ goto err;
+ if (EVP_MD_CTX_copy(new_ctx->ctx, ctx->ctx) != 1)
+ goto err;
+ if (EVP_DigestUpdate(new_ctx->ctx, data.data, data.size) != 1)
+ goto err;
ret = enif_make_resource(env, new_ctx);
- enif_release_resource(new_ctx);
CONSUME_REDS(env, data);
+ goto done;
+
+ bad_arg:
+ return enif_make_badarg(env);
+
+ err:
+ ret = atom_notsup;
+
+ done:
+ enif_release_resource(new_ctx);
return ret;
}