From: Vsevolod Stakhov Date: Wed, 5 Apr 2017 13:50:40 +0000 (+0100) Subject: [Fix] Check return values from Lua functions called from C X-Git-Tag: 1.5.5~45 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7c4ea9f998b92416e41cad22e8f7f15d9e07be3b;p=rspamd.git [Fix] Check return values from Lua functions called from C --- diff --git a/src/libstat/stat_process.c b/src/libstat/stat_process.c index 356b53807..0272c8fb7 100644 --- a/src/libstat/stat_process.c +++ b/src/libstat/stat_process.c @@ -179,6 +179,9 @@ rspamd_stat_tokenize_parts_metadata (struct rspamd_stat_ctx *st_ctx, lua_gettable (L, -2); if (lua_type (L, -1) == LUA_TTABLE) { + gint old_top; + + old_top = lua_gettop (L); lua_pushstring (L, "callback"); lua_gettable (L, -2); @@ -189,41 +192,47 @@ rspamd_stat_tokenize_parts_metadata (struct rspamd_stat_ctx *st_ctx, rspamd_lua_setclass (L, "rspamd{task}", -1); *ptask = task; - if (lua_pcall (L, 1, 1, 0) != 0) { + if (lua_pcall (L, 1, LUA_MULTRET, 0) != 0) { msg_err_task ("stat_metatokens failed: %s", lua_tostring (L, -1)); lua_pop (L, 1); } else { - /* Iterate over table of tables */ - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { - elt.flags |= RSPAMD_STAT_TOKEN_FLAG_LUA_META; - - if (lua_isnumber (L, -1)) { - gdouble num = lua_tonumber (L, -1); - guint8 *pnum = rspamd_mempool_alloc ( - task->task_pool, - sizeof (num)); - - msg_debug_task ("got metatoken number: %.2f", num); - memcpy (pnum, &num, sizeof (num)); - elt.begin = (gchar *) pnum; - elt.len = sizeof (num); - g_array_append_val (ar, elt); - } else if (lua_isstring (L, -1)) { - const gchar *str; - gsize tlen; - - str = lua_tolstring (L, -1, &tlen); - guint8 *pstr = rspamd_mempool_alloc ( - task->task_pool, - tlen); - memcpy (pstr, str, tlen); - - msg_debug_task ("got metatoken string: %*s", - (gint) tlen, str); - elt.begin = (gchar *) pstr; - elt.len = tlen; - g_array_append_val (ar, elt); + if (lua_gettop (L) > old_top && + lua_istable (L, old_top + 1)) { + lua_pushvalue (L, old_top + 1); + /* Iterate over table of tables */ + for (lua_pushnil (L); lua_next (L, -2); + lua_pop (L, 1)) { + elt.flags |= RSPAMD_STAT_TOKEN_FLAG_LUA_META; + + if (lua_isnumber (L, -1)) { + gdouble num = lua_tonumber (L, -1); + guint8 *pnum = rspamd_mempool_alloc ( + task->task_pool, + sizeof (num)); + + msg_debug_task ("got metatoken number: %.2f", + num); + memcpy (pnum, &num, sizeof (num)); + elt.begin = (gchar *) pnum; + elt.len = sizeof (num); + g_array_append_val (ar, elt); + } else if (lua_isstring (L, -1)) { + const gchar *str; + gsize tlen; + + str = lua_tolstring (L, -1, &tlen); + guint8 *pstr = rspamd_mempool_alloc ( + task->task_pool, + tlen); + memcpy (pstr, str, tlen); + + msg_debug_task ("got metatoken string: %*s", + (gint) tlen, str); + elt.begin = (gchar *) pstr; + elt.len = tlen; + g_array_append_val (ar, elt); + } } } } diff --git a/src/lua/lua_mimepart.c b/src/lua/lua_mimepart.c index 7f6ede7ef..581a2c260 100644 --- a/src/lua/lua_mimepart.c +++ b/src/lua/lua_mimepart.c @@ -935,6 +935,7 @@ lua_mimepart_headers_foreach (lua_State *L) struct rspamd_lua_regexp *re = NULL; GList *cur; struct rspamd_mime_header *hdr; + gint old_top; if (part && lua_isfunction (L, 2)) { if (lua_istable (L, 3)) { @@ -981,26 +982,29 @@ lua_mimepart_headers_foreach (lua_State *L) } } + old_top = lua_gettop (L); lua_pushvalue (L, 2); lua_pushstring (L, hdr->name); rspamd_lua_push_header (L, hdr, full, raw); - if (lua_pcall (L, 2, 1, 0) != 0) { + if (lua_pcall (L, 2, LUA_MULTRET, 0) != 0) { msg_err ("call to header_foreach failed: %s", lua_tostring (L, -1)); - lua_pop (L, 1); + lua_settop (L, old_top); break; } else { - if (lua_isboolean (L, -1)) { - if (lua_toboolean (L, -1)) { - lua_pop (L, 1); - break; + if (lua_gettop (L) > old_top) { + if (lua_isboolean (L, old_top + 1)) { + if (lua_toboolean (L, old_top + 1)) { + lua_settop (L, old_top); + break; + } } } } - lua_pop (L, 1); + lua_settop (L, old_top); cur = g_list_next (cur); } } diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index a3f0bc212..2ed2419e2 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -4006,6 +4006,7 @@ lua_task_headers_foreach (lua_State *L) struct rspamd_lua_regexp *re = NULL; GList *cur; struct rspamd_mime_header *hdr; + gint old_top; if (task && lua_isfunction (L, 2)) { if (lua_istable (L, 3)) { @@ -4052,26 +4053,29 @@ lua_task_headers_foreach (lua_State *L) } } + old_top = lua_gettop (L); lua_pushvalue (L, 2); lua_pushstring (L, hdr->name); rspamd_lua_push_header (L, hdr, full, raw); - if (lua_pcall (L, 2, 1, 0) != 0) { - msg_err_task ("call to header_foreach failed: %s", - lua_tostring (L, -1)); - lua_pop (L, 1); + if (lua_pcall (L, 2, LUA_MULTRET, 0) != 0) { + msg_err ("call to header_foreach failed: %s", + lua_tostring (L, -1)); + lua_settop (L, old_top); break; } else { - if (lua_isboolean (L, -1)) { - if (lua_toboolean (L, -1)) { - lua_pop (L, 1); - break; + if (lua_gettop (L) > old_top) { + if (lua_isboolean (L, old_top + 1)) { + if (lua_toboolean (L, old_top + 1)) { + lua_settop (L, old_top); + break; + } } } } - lua_pop (L, 1); + lua_settop (L, old_top); cur = g_list_next (cur); } }