From 8d8ceecc8b9506c031fd711e4c4ded0848f9539d Mon Sep 17 00:00:00 2001 From: Gregory Burd Date: Fri, 19 Apr 2013 09:11:41 -0400 Subject: [PATCH] enif_get_string can return < 1 when it copies less than the buffer size you pass into it, that'd result in a non-zero (aka true) test when in fact it's a problem if the argument isn't passed completely (however unlikely that is). enif_alloc_env() requires that later you enif_free_env() which I wasn't doing, this seems to keep memory steady in test runs. --- c_src/async_nif.h | 10 +++------- c_src/wterl.c | 26 +++++++++++++------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/c_src/async_nif.h b/c_src/async_nif.h index da13cd4..7601b6b 100644 --- a/c_src/async_nif.h +++ b/c_src/async_nif.h @@ -154,8 +154,6 @@ struct async_nif_state { #define ASYNC_NIF_WORK_ENV new_env #define ASYNC_NIF_REPLY(msg) enif_send(NULL, pid, env, enif_make_tuple2(env, ref, msg)) -// TODO: fix, currently NOREPLY() will block cause the recieve in async_nif.hrl wait forever -#define ASYNC_NIF_NOREPLY() enif_free_env(env) /** * TODO: @@ -246,11 +244,8 @@ async_nif_worker_fn(void *arg) /* and then call the post-work cleanup function. */ req->fn_post(req->args); - /* Note: we don't call enif_free_env(req->env) because somewhere in the - work block there has been a call to enif_send() which invalidates the - environment we created which in theory will cause the next GC to free - it for us). If a work block doesn't call ASYNC_NIF_REPLY() at some - point then it must call ASYNC_NIF_NOREPLY() to free this env. */ + /* Free all resources allocated for this async request. */ + enif_free_env(req->env); enif_free(req->args); enif_free(req); req = NULL; @@ -301,6 +296,7 @@ async_nif_unload(ErlNifEnv *env, struct async_nif_state *async_nif) enif_make_tuple2(req->env, enif_make_atom(req->env, "error"), enif_make_atom(req->env, "shutdown"))); req->fn_post(req->args); + enif_free_env(req->env); enif_free(req->args); enif_free(req); }); diff --git a/c_src/wterl.c b/c_src/wterl.c index 4a8992c..764db6d 100644 --- a/c_src/wterl.c +++ b/c_src/wterl.c @@ -299,7 +299,7 @@ ASYNC_NIF_DECL( { // pre if (!(argc == 3 && - enif_get_string(env, argv[0], args->homedir, sizeof args->homedir, ERL_NIF_LATIN1) && + (enif_get_string(env, argv[0], args->homedir, sizeof args->homedir, ERL_NIF_LATIN1) > 0) && enif_is_binary(env, argv[1]) && enif_is_binary(env, argv[2]))) { ASYNC_NIF_RETURN_BADARG(); @@ -448,7 +448,7 @@ ASYNC_NIF_DECL( if (!(argc == 3 && enif_get_resource(env, argv[0], wterl_conn_RESOURCE, (void**)&args->conn_handle) && - enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) && + (enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) > 0) && enif_is_binary(env, argv[2]))) { ASYNC_NIF_RETURN_BADARG(); } @@ -502,7 +502,7 @@ ASYNC_NIF_DECL( if (!(argc == 3 && enif_get_resource(env, argv[0], wterl_conn_RESOURCE, (void**)&args->conn_handle) && - enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) && + (enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) > 0) && enif_is_binary(env, argv[2]))) { ASYNC_NIF_RETURN_BADARG(); } @@ -568,8 +568,8 @@ ASYNC_NIF_DECL( if (!(argc == 4 && enif_get_resource(env, argv[0], wterl_conn_RESOURCE, (void**)&args->conn_handle) && - enif_get_string(env, argv[1], args->oldname, sizeof args->oldname, ERL_NIF_LATIN1) && - enif_get_string(env, argv[2], args->newname, sizeof args->newname, ERL_NIF_LATIN1) && + (enif_get_string(env, argv[1], args->oldname, sizeof args->oldname, ERL_NIF_LATIN1) > 0) && + (enif_get_string(env, argv[2], args->newname, sizeof args->newname, ERL_NIF_LATIN1) > 0) && enif_is_binary(env, argv[3]))) { ASYNC_NIF_RETURN_BADARG(); } @@ -635,7 +635,7 @@ ASYNC_NIF_DECL( if (!(argc == 3 && enif_get_resource(env, argv[0], wterl_conn_RESOURCE, (void**)&args->conn_handle) && - enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) && + (enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) > 0) && enif_is_binary(env, argv[2]))) { ASYNC_NIF_RETURN_BADARG(); } @@ -746,7 +746,7 @@ ASYNC_NIF_DECL( if (!(argc == 5 && enif_get_resource(env, argv[0], wterl_conn_RESOURCE, (void**)&args->conn_handle) && - enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) && + (enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) > 0) && enif_is_binary(env, argv[4]))) { ASYNC_NIF_RETURN_BADARG(); } @@ -921,7 +921,7 @@ ASYNC_NIF_DECL( if (!(argc == 3 && enif_get_resource(env, argv[0], wterl_conn_RESOURCE, (void**)&args->conn_handle) && - enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) && + (enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) > 0) && enif_is_binary(env, argv[2]))) { ASYNC_NIF_RETURN_BADARG(); } @@ -983,7 +983,7 @@ ASYNC_NIF_DECL( if (!(argc == 3 && enif_get_resource(env, argv[0], wterl_conn_RESOURCE, (void**)&args->conn_handle) && - enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) && + (enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) > 0) && enif_is_binary(env, argv[2]))) { ASYNC_NIF_RETURN_BADARG(); } @@ -1044,7 +1044,7 @@ ASYNC_NIF_DECL( if (!(argc == 3 && enif_get_resource(env, argv[0], wterl_conn_RESOURCE, (void**)&args->conn_handle) && - enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) && + (enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) > 0) && enif_is_binary(env, argv[2]))) { ASYNC_NIF_RETURN_BADARG(); } @@ -1099,7 +1099,7 @@ ASYNC_NIF_DECL( if (!(argc == 3 && enif_get_resource(env, argv[0], wterl_conn_RESOURCE, (void**)&args->conn_handle) && - enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) && + (enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) > 0) && enif_is_binary(env, argv[2]))) { ASYNC_NIF_RETURN_BADARG(); } @@ -1170,7 +1170,7 @@ ASYNC_NIF_DECL( if (!(argc == 4 && enif_get_resource(env, argv[0], wterl_conn_RESOURCE, (void**)&args->conn_handle) && - enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) && + (enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) > 0) && enif_is_binary(env, argv[2]) && enif_is_binary(env, argv[3]))) { ASYNC_NIF_RETURN_BADARG(); @@ -1236,7 +1236,7 @@ ASYNC_NIF_DECL( if (!(argc == 3 && enif_get_resource(env, argv[0], wterl_conn_RESOURCE, (void**)&args->conn_handle) && - enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) && + (enif_get_string(env, argv[1], args->uri, sizeof args->uri, ERL_NIF_LATIN1) > 0) && enif_is_binary(env, argv[2]))) { ASYNC_NIF_RETURN_BADARG(); }