aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crypto/c_src/info.c
diff options
context:
space:
mode:
authorDoug Hogan <[email protected]>2019-01-03 18:59:40 -0800
committerDoug Hogan <[email protected]>2019-01-08 00:08:22 -0800
commit03889bc9dda3cf7e6cbacc11ef8e964982cd296e (patch)
treee2e6d4266e7153b4faa774000fbda87f020daa28 /lib/crypto/c_src/info.c
parentc41e0bb6d6981ab6fcf33365efa6ddd194fddd39 (diff)
downloadotp-03889bc9dda3cf7e6cbacc11ef8e964982cd296e.tar.gz
otp-03889bc9dda3cf7e6cbacc11ef8e964982cd296e.tar.bz2
otp-03889bc9dda3cf7e6cbacc11ef8e964982cd296e.zip
Revamp info_lib()
* Add error checking and use enif_make_badarg() on error. * Use size_t when using sizeof(). * Move initialization away from declaration so it's not as easy to miss.
Diffstat (limited to 'lib/crypto/c_src/info.c')
-rw-r--r--lib/crypto/c_src/info.c37
1 files changed, 28 insertions, 9 deletions
diff --git a/lib/crypto/c_src/info.c b/lib/crypto/c_src/info.c
index d4e230dffd..2411968b95 100644
--- a/lib/crypto/c_src/info.c
+++ b/lib/crypto/c_src/info.c
@@ -62,16 +62,26 @@ void error_handler(void* null, const char* errstr)
}
#endif /* HAVE_DYNAMIC_CRYPTO_LIB */
-ERL_NIF_TERM info_lib(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
-{
+ERL_NIF_TERM info_lib(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
+{/* () */
/* [{<<"OpenSSL">>,9470143,<<"OpenSSL 0.9.8k 25 Mar 2009">>}] */
- static const char libname[] = "OpenSSL";
- unsigned name_sz = strlen(libname);
- const char* ver = SSLeay_version(SSLEAY_VERSION);
- unsigned ver_sz = strlen(ver);
ERL_NIF_TERM name_term, ver_term;
- int ver_num = OPENSSL_VERSION_NUMBER;
+ static const char libname[] = "OpenSSL";
+ size_t name_sz;
+ const char* ver;
+ size_t ver_sz;
+ int ver_num;
+ unsigned char *out_name, *out_ver;
+
+ if (argc != 0)
+ goto bad_arg;
+
+ name_sz = strlen(libname);
+ ver = SSLeay_version(SSLEAY_VERSION);
+ ver_sz = strlen(ver);
+ ver_num = OPENSSL_VERSION_NUMBER;
+
/* R16:
* Ignore library version number from SSLeay() and instead show header
* version. Otherwise user might try to call a function that is implemented
@@ -81,10 +91,19 @@ ERL_NIF_TERM info_lib(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
* Version string is still from library though.
*/
- memcpy(enif_make_new_binary(env, name_sz, &name_term), libname, name_sz);
- memcpy(enif_make_new_binary(env, ver_sz, &ver_term), ver, ver_sz);
+ if ((out_name = enif_make_new_binary(env, name_sz, &name_term)) == NULL)
+ goto err;
+ if ((out_ver = enif_make_new_binary(env, ver_sz, &ver_term)) == NULL)
+ goto err;
+
+ memcpy(out_name, libname, name_sz);
+ memcpy(out_ver, ver, ver_sz);
return enif_make_list1(env, enif_make_tuple3(env, name_term,
enif_make_int(env, ver_num),
ver_term));
+
+ bad_arg:
+ err:
+ return enif_make_badarg(env);
}