From 0e270dbe709e743ec840999c05e35f4d63be69b4 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sun, 15 Jan 2017 20:23:35 +0000 Subject: [Fix] Fix couple of issues Found by: Coverity --- src/lua/lua_cryptobox.c | 4 ++++ src/lua/lua_fann.c | 2 ++ src/lua/lua_html.c | 4 ++-- src/lua/lua_map.c | 5 +++-- src/lua/lua_redis.c | 28 +++++++++++++++------------- src/lua/lua_trie.c | 6 +++--- 6 files changed, 29 insertions(+), 20 deletions(-) (limited to 'src/lua') diff --git a/src/lua/lua_cryptobox.c b/src/lua/lua_cryptobox.c index 94dec34f5..534ffc0e5 100644 --- a/src/lua/lua_cryptobox.c +++ b/src/lua/lua_cryptobox.c @@ -1131,6 +1131,10 @@ lua_cryptobox_verify_file (lua_State *L) } } else { + if (map != NULL) { + munmap (map, len); + } + return luaL_error (L, "invalid arguments"); } diff --git a/src/lua/lua_fann.c b/src/lua/lua_fann.c index ba57b414a..884a40293 100644 --- a/src/lua/lua_fann.c +++ b/src/lua/lua_fann.c @@ -213,6 +213,8 @@ rspamd_fann_create_train (guint num_data, guint num_input, guint num_output) fann_type *inp, *outp; guint i; + g_assert (num_data > 0 && num_input > 0 && num_output > 0); + t = calloc (1, sizeof (*t)); g_assert (t != NULL); diff --git a/src/lua/lua_html.c b/src/lua/lua_html.c index 0fdd61929..0ec561338 100644 --- a/src/lua/lua_html.c +++ b/src/lua/lua_html.c @@ -374,9 +374,9 @@ lua_html_get_blocks (lua_State *L) guint i; if (hc != NULL) { - lua_createtable (L, hc->blocks->len, 0); - if (hc->blocks && hc->blocks->len > 0) { + lua_createtable (L, hc->blocks->len, 0); + for (i = 0; i < hc->blocks->len; i ++) { bl = g_ptr_array_index (hc->blocks, i); lua_html_push_block (L, bl); diff --git a/src/lua/lua_map.c b/src/lua/lua_map.c index 1ed4aff7d..f4d3a6b88 100644 --- a/src/lua/lua_map.c +++ b/src/lua/lua_map.c @@ -356,8 +356,9 @@ lua_map_fin (struct map_cb_data *data) lua_pop (cbdata->L, 1); } } - - cbdata->data = rspamd_fstring_assign (cbdata->data, "", 0); + else if (cbdata->data != NULL) { + cbdata->data = rspamd_fstring_assign (cbdata->data, "", 0); + } } gint diff --git a/src/lua/lua_redis.c b/src/lua/lua_redis.c index 2307db835..acb355faa 100644 --- a/src/lua/lua_redis.c +++ b/src/lua/lua_redis.c @@ -533,6 +533,7 @@ rspamd_lua_redis_prepare_connection (lua_State *L, gint *pcbref) if (lua_istable (L, 1)) { /* Table version */ + lua_pushvalue (L, 1); lua_pushstring (L, "task"); lua_gettable (L, -2); if (lua_type (L, -1) == LUA_TUSERDATA) { @@ -621,6 +622,7 @@ rspamd_lua_redis_prepare_connection (lua_State *L, gint *pcbref) dbname = lua_tostring (L, -1); } lua_pop (L, 1); + lua_pop (L, 1); /* table */ if (ret && addr != NULL) { @@ -817,6 +819,7 @@ lua_redis_make_request_sync (lua_State *L) redisReply *r; if (lua_istable (L, 1)) { + lua_pushvalue (L, 1); lua_pushstring (L, "cmd"); lua_gettable (L, -2); @@ -848,9 +851,13 @@ lua_redis_make_request_sync (lua_State *L) } lua_pop (L, 1); - lua_pushstring (L, "args"); - lua_gettable (L, -2); - lua_redis_parse_args (L, -1, cmd, &args, &arglens, &nargs); + if (cmd) { + lua_pushstring (L, "args"); + lua_gettable (L, -2); + lua_redis_parse_args (L, -1, cmd, &args, &arglens, &nargs); + lua_pop (L, 1); + } + lua_pop (L, 1); if (addr && cmd) { @@ -928,7 +935,6 @@ lua_redis_connect (lua_State *L) struct lua_redis_userdata *ud; struct lua_redis_ctx *ctx, **pctx; gdouble timeout = REDIS_DEFAULT_TIMEOUT; - gboolean ret = FALSE; ctx = rspamd_lua_redis_prepare_connection (L, NULL); @@ -936,10 +942,11 @@ lua_redis_connect (lua_State *L) ud = &ctx->d.async; lua_pushstring (L, "timeout"); - lua_gettable (L, -2); + lua_gettable (L, 1); if (lua_type (L, -1) == LUA_TNUMBER) { timeout = lua_tonumber (L, -1); } + lua_pop (L, 1); ud->timeout = timeout; } @@ -950,14 +957,9 @@ lua_redis_connect (lua_State *L) return 2; } - if (ret) { - pctx = lua_newuserdata (L, sizeof (ctx)); - *pctx = ctx; - rspamd_lua_setclass (L, "rspamd{redis}", -1); - } - else { - lua_pushnil (L); - } + pctx = lua_newuserdata (L, sizeof (ctx)); + *pctx = ctx; + rspamd_lua_setclass (L, "rspamd{redis}", -1); return 1; } diff --git a/src/lua/lua_trie.c b/src/lua/lua_trie.c index 0531197a2..5911842b9 100644 --- a/src/lua/lua_trie.c +++ b/src/lua/lua_trie.c @@ -256,7 +256,7 @@ lua_trie_search_mime (lua_State *L) gsize len, i; gboolean found = FALSE; - if (trie) { + if (trie && task) { for (i = 0; i < task->text_parts->len; i ++) { part = g_ptr_array_index (task->text_parts, i); @@ -292,7 +292,7 @@ lua_trie_search_rawmsg (lua_State *L) gsize len; gboolean found = FALSE; - if (trie) { + if (trie && task) { text = task->msg.begin; len = task->msg.len; @@ -322,7 +322,7 @@ lua_trie_search_rawbody (lua_State *L) gsize len; gboolean found = FALSE; - if (trie) { + if (trie && task) { if (task->raw_headers_content.len > 0) { text = task->msg.begin + task->raw_headers_content.len; len = task->msg.len - task->raw_headers_content.len; -- cgit v1.2.3