From 59f56bfbf59492ef987bd702c3ce59cc113f47c0 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 14 Sep 2015 21:56:30 +0100 Subject: [PATCH] Improve error handler for lua_pcall. --- src/lua/lua_common.c | 17 +++++++++-------- src/lua/lua_config.c | 12 +++++------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index 3841ffe14..209e68fb1 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -305,9 +305,7 @@ rspamd_init_lua_filters (struct rspamd_config *cfg) continue; } - tb = g_string_new (""); - lua_pushlightuserdata (L, tb); - lua_pushcclosure (L, &rspamd_lua_traceback, 1); + lua_pushcfunction (L, &rspamd_lua_traceback); err_idx = lua_gettop (L); if (luaL_loadfile (L, module->path) != 0) { @@ -315,7 +313,7 @@ rspamd_init_lua_filters (struct rspamd_config *cfg) lua_tostring (L, -1)); cur = g_list_next (cur); g_string_free (tb, TRUE); - lua_pop (L, 1); /* Error function */ + lua_pop (L, 1); /* Error function */ continue; } @@ -326,16 +324,16 @@ rspamd_init_lua_filters (struct rspamd_config *cfg) lua_setglobal (L, "rspamd_config"); if (lua_pcall (L, 0, 0, err_idx) != 0) { + tb = lua_touserdata (L, -1); msg_err_config ("init of %s failed: %v", module->path, tb); cur = g_list_next (cur); g_string_free (tb, TRUE); - lua_pop (L, 1); + lua_pop (L, 2); /* Result and error function */ continue; } - g_string_free (tb, TRUE); lua_pop (L, 1); /* Error function */ } cur = g_list_next (cur); @@ -817,10 +815,11 @@ gint rspamd_lua_traceback (lua_State *L) { lua_Debug d; - GString *tb = lua_touserdata (L, lua_upvalueindex(1)); + GString *tb; const gchar *msg = lua_tostring (L, 1); gint i = 1; + tb = g_string_sized_new (100); g_string_append_printf (tb, "%s; trace:", msg); while (lua_getstack (L, i++, &d)) { @@ -830,5 +829,7 @@ rspamd_lua_traceback (lua_State *L) (d.name ? d.name : ""), d.what); } - return 0; + lua_pushlightuserdata (L, tb); + + return 1; } diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index 5f42a528c..a26b6b4aa 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -876,9 +876,7 @@ lua_metric_symbol_callback (struct rspamd_task *task, gpointer ud) lua_State *L = cd->L; GString *tb; - tb = g_string_new (""); - lua_pushlightuserdata (L, tb); - lua_pushcclosure (L, &rspamd_lua_traceback, 1); + lua_pushcfunction (L, &rspamd_lua_traceback); err_idx = lua_gettop (L); level ++; @@ -895,9 +893,10 @@ lua_metric_symbol_callback (struct rspamd_task *task, gpointer ud) *ptask = task; if (lua_pcall (L, 1, LUA_MULTRET, err_idx) != 0) { - - msg_err_task ("call to (%s)%s failed: %v", cd->symbol, - cd->cb_is_ref ? "local function" : cd->callback.name, tb); + tb = lua_touserdata (L, -1); + msg_err_task ("call to (%s) failed: %v", cd->symbol, tb); + g_string_free (tb, TRUE); + lua_pop (L, 1); } else { nresults = lua_gettop (L) - level; @@ -936,7 +935,6 @@ lua_metric_symbol_callback (struct rspamd_task *task, gpointer ud) } } - g_string_free (tb, TRUE); lua_pop (L, 1); /* Error function */ } -- 2.39.5