]> source.dussan.org Git - rspamd.git/commitdiff
[Fix] Check return values from Lua functions called from C
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 5 Apr 2017 13:50:40 +0000 (14:50 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 5 Apr 2017 13:50:40 +0000 (14:50 +0100)
src/libstat/stat_process.c
src/lua/lua_mimepart.c
src/lua/lua_task.c

index 356b538070bee690535f3e43bd8d47745d7178c6..0272c8fb7dc751171d1c8e67fadd6e6615106cda 100644 (file)
@@ -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);
+                                                       }
                                                }
                                        }
                                }
index 7f6ede7ef493a350ecb3f2d8bbee8679ae32fe52..581a2c260cbe1f9e5e442a129c110234dcf78eee 100644 (file)
@@ -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);
                        }
                }
index a3f0bc212a43bf813444d187c4f320be973fa7b9..2ed2419e2ef05158e0dfce73f19c19fa4a6a7cce 100644 (file)
@@ -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);
                        }
                }