From bf4d61680c7fc593fc8f83e9ea329f2f5ecfa786 Mon Sep 17 00:00:00 2001 From: Zane Beckwith Date: Mon, 14 Aug 2017 17:01:50 +0000 Subject: [PATCH] Change size_t variables to unsigned int's. These variables are being initialized via calls to `enif_get_uint`, so it's safer to declare them as unsigned int's rather than size_t's. Their being used in calls to `enif_alloc_binary`, which takes a size_t as its size. However, the resulting ErlNifBinary keeps its size as an unsigned int, so asking for a size that's an unsigned int should be safe. This would be problematic in the case where sizeof(size_t) < sizeof(unsigned), which would mean we're getting fewer bytes allocated than expected. Perhaps an explicit check for, for example, `hashSize > MAX_SIZE` would be good here? --- c_src/enacl_nif.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/c_src/enacl_nif.c b/c_src/enacl_nif.c index f4d54d9..1046af7 100644 --- a/c_src/enacl_nif.c +++ b/c_src/enacl_nif.c @@ -1039,7 +1039,7 @@ ERL_NIF_TERM enif_crypto_onetimeauth_verify(ErlNifEnv *env, int argc, ERL_NIF_TE static ERL_NIF_TERM enif_randombytes(ErlNifEnv *env, int argc, ERL_NIF_TERM const argv[]) { - size_t req_size; + unsigned req_size; ErlNifBinary result; if ((argc != 1) || (!enif_get_uint(env, argv[0], &req_size))) { @@ -1350,7 +1350,7 @@ static ERL_NIF_TERM enif_crypto_generichash(ErlNifEnv *env, int argc, ERL_NIF_TERM const argv[]) { ErlNifBinary hash, message, key; - size_t hashSize; + unsigned hashSize; // Validate the arguments if( (argc != 3) || @@ -1395,7 +1395,7 @@ static ERL_NIF_TERM enif_crypto_generichash_init(ErlNifEnv *env, int argc, ERL_NIF_TERM const argv[]) { ErlNifBinary key; - size_t hashSize; + unsigned hashSize; // Validate the arguments if( (argc != 2) || @@ -1447,7 +1447,7 @@ static ERL_NIF_TERM enif_crypto_generichash_update(ErlNifEnv *env, int argc, ERL_NIF_TERM const argv[]) { ErlNifBinary message; - size_t hashSize; + unsigned hashSize; crypto_generichash_state *state; @@ -1484,7 +1484,7 @@ static ERL_NIF_TERM enif_crypto_generichash_final(ErlNifEnv *env, int argc, ERL_NIF_TERM const argv[]) { ErlNifBinary hash; - size_t hashSize; + unsigned hashSize; crypto_generichash_state *state;