From eb5fc65aca905ad38bab0dae85dba0f9a56d7766 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 24 May 2019 14:13:13 +0100 Subject: [PATCH] [Fix] Do not use lightuserdata for traceback LuaJIT limits lightuserdata usage to 47 bits. On Arm64, this leads to break of the C <-> Lua interoperability using this type. This rework has changed traceback function behaviour from lightuserdata opaque pointer (GString * in particular) to luaL_Buffer. Issue: #2906 --- src/libmime/message.c | 7 +-- src/libmime/mime_expressions.c | 9 +-- src/libserver/cfg_rcl.c | 35 +++-------- src/libserver/cfg_utils.c | 8 +-- src/libserver/re_cache.c | 9 +-- src/libstat/backends/redis_backend.c | 8 +-- src/libstat/backends/sqlite3_backend.c | 16 ++--- src/libstat/stat_config.c | 9 +-- src/libstat/stat_process.c | 13 +---- src/lua/lua_common.c | 81 ++++++++++++-------------- src/lua/lua_common.h | 3 +- src/lua/lua_config.c | 51 ++++------------ src/lua/lua_dns_resolver.c | 9 +-- src/lua/lua_expression.c | 4 +- src/lua/lua_task.c | 4 +- src/lua/lua_thread_pool.c | 18 +++--- src/lua/lua_upstream.c | 4 +- src/lua/lua_worker.c | 24 ++++---- src/plugins/dkim_check.c | 7 +-- src/plugins/fuzzy_check.c | 28 ++------- src/plugins/surbl.c | 4 +- src/rspamd_proxy.c | 20 ++----- 22 files changed, 117 insertions(+), 254 deletions(-) diff --git a/src/libmime/message.c b/src/libmime/message.c index 6211ea2f3..cd3772c78 100644 --- a/src/libmime/message.c +++ b/src/libmime/message.c @@ -716,11 +716,8 @@ rspamd_message_process_plain_text_part (struct rspamd_task *task, text_part->utf_raw_content->len); if (lua_pcall (L, 1, 1, err_idx) != 0) { - GString *tb; - - tb = lua_touserdata (L, -1); - msg_err_task ("cannot call lua lua_ical.ical_txt_values: %s", tb->str); - g_string_free (tb, TRUE); + msg_err_task ("cannot call lua lua_ical.ical_txt_values: %s", + lua_tostring (L, -1)); lua_settop (L, err_idx - 1); return FALSE; diff --git a/src/libmime/mime_expressions.c b/src/libmime/mime_expressions.c index 4a0071f28..7a0c27f1b 100644 --- a/src/libmime/mime_expressions.c +++ b/src/libmime/mime_expressions.c @@ -1114,7 +1114,6 @@ rspamd_mime_expr_process (void *ud, rspamd_expression_atom_t *atom) } else if (mime_atom->type == MIME_ATOM_LOCAL_LUA_FUNCTION) { gint err_idx; - GString *tb; L = task->cfg->lua_state; lua_pushcfunction (L, &rspamd_lua_traceback); @@ -1124,13 +1123,9 @@ rspamd_mime_expr_process (void *ud, rspamd_expression_atom_t *atom) rspamd_lua_task_push (L, task); if (lua_pcall (L, 1, 1, err_idx) != 0) { - tb = lua_touserdata (L, -1); - msg_info_task ("lua call to local function for atom '%s' failed: %v", + msg_info_task ("lua call to local function for atom '%s' failed: %s", mime_atom->str, - tb); - if (tb) { - g_string_free (tb, TRUE); - } + lua_tostring (L, -1)); } else { if (lua_type (L, -1) == LUA_TBOOLEAN) { diff --git a/src/libserver/cfg_rcl.c b/src/libserver/cfg_rcl.c index 9df9e4aa1..d5e30d8a7 100644 --- a/src/libserver/cfg_rcl.c +++ b/src/libserver/cfg_rcl.c @@ -753,7 +753,6 @@ rspamd_rcl_lua_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, ucl_object_tostring (obj)); gchar *cur_dir, *lua_dir, *lua_file, *tmp1, *tmp2; lua_State *L = cfg->lua_state; - GString *tb; gint err_idx; tmp1 = g_strdup (lua_src); @@ -788,15 +787,13 @@ rspamd_rcl_lua_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, /* Now do it */ if (lua_pcall (L, 0, 0, err_idx) != 0) { - tb = lua_touserdata (L, -1); g_set_error (err, CFG_RCL_ERROR, EINVAL, "cannot init lua file %s: %s", lua_src, - tb->str); - g_string_free (tb, TRUE); - lua_pop (L, 2); + lua_tostring (L, -1)); + lua_settop (L, 0); if (chdir (cur_dir) == -1) { msg_err_config ("cannot chdir to %s: %s", cur_dir, @@ -1241,7 +1238,6 @@ rspamd_rcl_classifier_handler (rspamd_mempool_t *pool, const gchar *lua_script; gsize slen; gint err_idx, ref_idx; - GString *tb = NULL; lua_script = ucl_object_tolstring (cur, &slen); L = cfg->lua_state; @@ -1263,13 +1259,11 @@ rspamd_rcl_classifier_handler (rspamd_mempool_t *pool, /* Now do it */ if (lua_pcall (L, 0, 1, err_idx) != 0) { - tb = lua_touserdata (L, -1); g_set_error (err, CFG_RCL_ERROR, EINVAL, "cannot init lua condition script: %s", - tb->str); - g_string_free (tb, TRUE); + lua_tostring (L, -1)); lua_settop (L, 0); return FALSE; @@ -3484,7 +3478,6 @@ rspamd_rcl_maybe_apply_lua_transform (struct rspamd_config *cfg) { lua_State *L = cfg->lua_state; gint err_idx, ret; - GString *tb; gchar str[PATH_MAX]; static const char *transform_script = "lua_cfg_transform"; @@ -3518,13 +3511,8 @@ rspamd_rcl_maybe_apply_lua_transform (struct rspamd_config *cfg) ucl_object_push_lua (L, cfg->rcl_obj, true); if ((ret = lua_pcall (L, 1, 2, err_idx)) != 0) { - tb = lua_touserdata (L, -1); - msg_err ("call to rspamadm lua script failed (%d): %v", ret, tb); - - if (tb) { - g_string_free (tb, TRUE); - } - + msg_err ("call to rspamadm lua script failed (%d): %v", ret, + lua_tostring (L, -1)); lua_settop (L, 0); return; @@ -3588,11 +3576,8 @@ rspamd_rcl_jinja_handler (struct ucl_parser *parser, lua_pushboolean (L, false); if (lua_pcall (L, 3, 1, err_idx) != 0) { - GString *tb; - - tb = lua_touserdata (L, -1); - msg_err_config ("cannot call lua jinja_template script: %s", tb->str); - g_string_free (tb, TRUE); + msg_err_config ("cannot call lua jinja_template script: %s", + lua_tostring (L, -1)); lua_settop (L, err_idx - 1); return false; @@ -3856,12 +3841,8 @@ rspamd_config_read (struct rspamd_config *cfg, rspamd_lua_setclass (L, "rspamd{config}", -1); if (lua_pcall (L, 1, 0, err_idx) != 0) { - GString *tb; - - tb = lua_touserdata (L, -1); msg_err_config ("cannot call lua init_debug_logging script: %s", - tb->str); - g_string_free (tb, TRUE); + lua_tostring (L, -1)); lua_settop (L, err_idx - 1); return FALSE; diff --git a/src/libserver/cfg_utils.c b/src/libserver/cfg_utils.c index 4d717c860..586625184 100644 --- a/src/libserver/cfg_utils.c +++ b/src/libserver/cfg_utils.c @@ -829,12 +829,8 @@ rspamd_config_post_load (struct rspamd_config *cfg, if (rspamd_lua_require_function (cfg->lua_state, "lua_squeeze_rules", "squeeze_init")) { if (lua_pcall (L, 0, 0, err_idx) != 0) { - GString *tb = lua_touserdata (L, -1); - msg_err_config ("call to squeeze_init script failed: %v", tb); - - if (tb) { - g_string_free (tb, TRUE); - } + msg_err_config ("call to squeeze_init script failed: %s", + lua_tostring (L, -1)); } } diff --git a/src/libserver/re_cache.c b/src/libserver/re_cache.c index f4f190ed5..4f14f6ff8 100644 --- a/src/libserver/re_cache.c +++ b/src/libserver/re_cache.c @@ -801,7 +801,6 @@ rspamd_re_cache_process_selector (struct rspamd_task *task, khiter_t k; lua_State *L; gint err_idx, ret; - GString *tb; struct rspamd_task **ptask; gboolean result = FALSE; struct rspamd_re_cache *cache = rt->cache; @@ -845,13 +844,9 @@ rspamd_re_cache_process_selector (struct rspamd_task *task, rspamd_lua_setclass (L, "rspamd{task}", -1); if ((ret = lua_pcall (L, 1, 1, err_idx)) != 0) { - tb = lua_touserdata (L, -1); msg_err_task ("call to selector %s " - "failed (%d): %v", name, ret, tb); - - if (tb) { - g_string_free (tb, TRUE); - } + "failed (%d): %s", name, ret, + lua_tostring (L, -1)); } else { gsize slen; diff --git a/src/libstat/backends/redis_backend.c b/src/libstat/backends/redis_backend.c index d54767c12..5976968a7 100644 --- a/src/libstat/backends/redis_backend.c +++ b/src/libstat/backends/redis_backend.c @@ -148,7 +148,6 @@ rspamd_redis_expand_object (const gchar *pattern, struct rspamd_statfile_config *stcf; lua_State *L = NULL; struct rspamd_task **ptask; - GString *tb; const gchar *rcpt = NULL; gint err_idx; @@ -172,16 +171,15 @@ rspamd_redis_expand_object (const gchar *pattern, rspamd_lua_setclass (L, "rspamd{task}", -1); if (lua_pcall (L, 1, 1, err_idx) != 0) { - tb = lua_touserdata (L, -1); - msg_err_task ("call to user extraction script failed: %v", tb); - g_string_free (tb, TRUE); + msg_err_task ("call to user extraction script failed: %s", + lua_tostring (L, -1)); } else { rcpt = rspamd_mempool_strdup (task->task_pool, lua_tostring (L, -1)); } /* Result + error function */ - lua_pop (L, 2); + lua_settop (L, err_idx - 1); } if (rcpt) { diff --git a/src/libstat/backends/sqlite3_backend.c b/src/libstat/backends/sqlite3_backend.c index cf7291ee0..61bc5bbd3 100644 --- a/src/libstat/backends/sqlite3_backend.c +++ b/src/libstat/backends/sqlite3_backend.c @@ -314,7 +314,6 @@ rspamd_sqlite3_get_user (struct rspamd_stat_sqlite3_db *db, const gchar *user = NULL; struct rspamd_task **ptask; lua_State *L = db->L; - GString *tb; if (db->cbref_user == -1) { user = rspamd_task_get_principal_recipient (task); @@ -330,16 +329,15 @@ rspamd_sqlite3_get_user (struct rspamd_stat_sqlite3_db *db, rspamd_lua_setclass (L, "rspamd{task}", -1); if (lua_pcall (L, 1, 1, err_idx) != 0) { - tb = lua_touserdata (L, -1); - msg_err_task ("call to user extraction script failed: %v", tb); - g_string_free (tb, TRUE); + msg_err_task ("call to user extraction script failed: %s", + lua_tostring (L, -1)); } else { user = rspamd_mempool_strdup (task->task_pool, lua_tostring (L, -1)); } /* Result + error function */ - lua_pop (L, 2); + lua_settop (L, err_idx - 1); } @@ -377,7 +375,6 @@ rspamd_sqlite3_get_language (struct rspamd_stat_sqlite3_db *db, struct rspamd_mime_text_part *tp; struct rspamd_task **ptask; lua_State *L = db->L; - GString *tb; if (db->cbref_language == -1) { for (i = 0; i < task->text_parts->len; i++) { @@ -401,9 +398,8 @@ rspamd_sqlite3_get_language (struct rspamd_stat_sqlite3_db *db, rspamd_lua_setclass (L, "rspamd{task}", -1); if (lua_pcall (L, 1, 1, err_idx) != 0) { - tb = lua_touserdata (L, -1); - msg_err_task ("call to language extraction script failed: %v", tb); - g_string_free (tb, TRUE); + msg_err_task ("call to language extraction script failed: %s", + lua_tostring (L, -1)); } else { language = rspamd_mempool_strdup (task->task_pool, @@ -411,7 +407,7 @@ rspamd_sqlite3_get_language (struct rspamd_stat_sqlite3_db *db, } /* Result + error function */ - lua_pop (L, 2); + lua_settop (L, err_idx - 1); } diff --git a/src/libstat/stat_config.c b/src/libstat/stat_config.c index 101db4fe6..272a64ddf 100644 --- a/src/libstat/stat_config.c +++ b/src/libstat/stat_config.c @@ -189,7 +189,6 @@ rspamd_stat_init (struct rspamd_config *cfg, struct event_base *ev_base) else { /* Call this function to obtain closure */ gint err_idx, ret; - GString *tb; struct rspamd_config **pcfg; lua_pushcfunction (L, &rspamd_lua_traceback); @@ -201,13 +200,9 @@ rspamd_stat_init (struct rspamd_config *cfg, struct event_base *ev_base) rspamd_lua_setclass (L, "rspamd{config}", -1); if ((ret = lua_pcall (L, 1, 1, err_idx)) != 0) { - tb = lua_touserdata (L, -1); msg_err_config ("call to gen_stat_tokens lua " - "script failed (%d): %v", ret, tb); - - if (tb) { - g_string_free (tb, TRUE); - } + "script failed (%d): %s", ret, + lua_tostring (L, -1)); } else { if (lua_type (L, -1) != LUA_TFUNCTION) { diff --git a/src/libstat/stat_process.c b/src/libstat/stat_process.c index d097e12e0..901d62a2b 100644 --- a/src/libstat/stat_process.c +++ b/src/libstat/stat_process.c @@ -46,7 +46,6 @@ rspamd_stat_tokenize_parts_metadata (struct rspamd_stat_ctx *st_ctx, if (st_ctx->lua_stat_tokens_ref != -1) { gint err_idx, ret; - GString *tb; struct rspamd_task **ptask; lua_pushcfunction (L, &rspamd_lua_traceback); @@ -58,13 +57,8 @@ rspamd_stat_tokenize_parts_metadata (struct rspamd_stat_ctx *st_ctx, rspamd_lua_setclass (L, "rspamd{task}", -1); if ((ret = lua_pcall (L, 1, 1, err_idx)) != 0) { - tb = lua_touserdata (L, -1); msg_err_task ("call to stat_tokens lua " - "script failed (%d): %v", ret, tb); - - if (tb) { - g_string_free (tb, TRUE); - } + "script failed (%d): %s", ret, lua_tostring (L, -1)); } else { if (lua_type (L, -1) != LUA_TTABLE) { @@ -913,7 +907,6 @@ rspamd_stat_check_autolearn (struct rspamd_task *task) struct rspamd_metric_result *mres = NULL; struct rspamd_task **ptask; lua_State *L; - GString *tb; guint i; gint err_idx; gboolean ret = FALSE; @@ -1011,10 +1004,8 @@ rspamd_stat_check_autolearn (struct rspamd_task *task) rspamd_lua_setclass (L, "rspamd{task}", -1); if (lua_pcall (L, 1, 1, err_idx) != 0) { - tb = lua_touserdata (L, -1); msg_err_task ("call to autolearn script failed: " - "%v", tb); - g_string_free (tb, TRUE); + "%s", lua_tostring (L, -1)); } else { lua_ret = lua_tostring (L, -1); diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index 542726da4..c2940c9ab 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -510,13 +510,10 @@ rspamd_lua_load_env (lua_State *L, const char *fname, gint tbl_pos, GError **err } if (ret && lua_pcall (L, 0, 1, err_idx) != 0) { - GString *tb = lua_touserdata (L, -1); g_set_error (err, g_quark_from_static_string ("lua_env"), errno, "cannot init lua file %s: %s", fname, - tb->str); - g_string_free (tb, TRUE); - + lua_tostring (L, -1)); ret = FALSE; } @@ -1048,7 +1045,6 @@ rspamd_init_lua_filters (struct rspamd_config *cfg, gboolean force_load) GList *cur; struct script_module *module; lua_State *L = cfg->lua_state; - GString *tb; gint err_idx; cur = g_list_first (cfg->script_modules); @@ -1086,14 +1082,11 @@ rspamd_init_lua_filters (struct rspamd_config *cfg, gboolean force_load) 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", + msg_err_config ("init of %s failed: %s", module->path, - tb); - - g_string_free (tb, TRUE); - lua_pop (L, 2); /* Result and error function */ + lua_tostring (L, -1)); + lua_settop (L, err_idx - 1); rspamd_plugins_table_push_elt (L, "disabled_failed", module->name); @@ -1667,44 +1660,41 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos, } static void -rspamd_lua_traceback_string (lua_State *L, GString *s) +rspamd_lua_traceback_string (lua_State *L, luaL_Buffer *buf) { - gint i = 1; + gint i = 1, r; lua_Debug d; + gchar tmp[256]; while (lua_getstack (L, i++, &d)) { lua_getinfo (L, "nSl", &d); - g_string_append_printf (s, " [%d]:{%s:%d - %s [%s]};", + r = rspamd_snprintf (tmp, sizeof (tmp), " [%d]:{%s:%d - %s [%s]};", i - 1, d.short_src, d.currentline, (d.name ? d.name : ""), d.what); + luaL_addlstring (buf, tmp, r); } } gint rspamd_lua_traceback (lua_State *L) { + luaL_Buffer b; - GString *tb; - - tb = rspamd_lua_get_traceback_string (L); - - lua_pushlightuserdata (L, tb); + luaL_buffinit (L, &b); + rspamd_lua_get_traceback_string (L, &b); + luaL_pushresult (&b); return 1; } -GString * -rspamd_lua_get_traceback_string (lua_State *L) +void +rspamd_lua_get_traceback_string (lua_State *L, luaL_Buffer *buf) { - GString *tb; const gchar *msg = lua_tostring (L, -1); - tb = g_string_sized_new (100); - g_string_append_printf (tb, "%s; trace:", msg); - - rspamd_lua_traceback_string (L, tb); - - return tb; + luaL_addstring (buf, msg); + luaL_addstring (buf, "; trace:"); + rspamd_lua_traceback_string (L, buf); } guint @@ -1730,7 +1720,6 @@ rspamd_lua_check_udata_common (lua_State *L, gint pos, const gchar *classname, gboolean fatal) { void *p = lua_touserdata (L, pos); - GString *err_msg; guint i, top = lua_gettop (L); if (p == NULL) { @@ -1769,12 +1758,19 @@ err: actual_classname = lua_typename (L, lua_type (L, pos)); } - err_msg = g_string_sized_new (100); - rspamd_printf_gstring (err_msg, "expected %s at position %d, but userdata has " - "%s metatable; trace: ", + luaL_Buffer buf; + gchar tmp[512]; + gint r; + + luaL_buffinit (L, &buf); + r = rspamd_snprintf (tmp, sizeof (tmp), + "expected %s at position %d, but userdata has " + "%s metatable; trace: ", classname, pos, actual_classname); - rspamd_lua_traceback_string (L, err_msg); - rspamd_printf_gstring (err_msg, " stack(%d): ", top); + luaL_addlstring (&buf, tmp, r); + rspamd_lua_traceback_string (L, &buf); + r = rspamd_snprintf (tmp, sizeof (tmp), " stack(%d): ", top); + luaL_addlstring (&buf, tmp, r); for (i = 1; i <= MIN (top, 10); i ++) { if (lua_type (L, i) == LUA_TUSERDATA) { @@ -1791,17 +1787,19 @@ err: clsname = lua_typename (L, lua_type (L, i)); } - rspamd_printf_gstring (err_msg, "[%d: ud=%s] ", i, + r = rspamd_snprintf (tmp, sizeof (tmp), "[%d: ud=%s] ", i, clsname); + luaL_addlstring (&buf, tmp, r); } else { - rspamd_printf_gstring (err_msg, "[%d: %s] ", i, + r = rspamd_snprintf (tmp, sizeof (tmp), "[%d: %s] ", i, lua_typename (L, lua_type (L, i))); + luaL_addlstring (&buf, tmp, r); } } - msg_err ("lua type error: %v", err_msg); - g_string_free (err_msg, TRUE); + luaL_pushresult (&buf); + msg_err ("lua type error: %s", lua_tostring (L, -1)); } lua_settop (L, top); @@ -1982,11 +1980,8 @@ rspamd_lua_try_load_redis (lua_State *L, const ucl_object_t *obj, lua_pushboolean (L, false); /* no_fallback */ if (lua_pcall (L, 3, 1, err_idx) != 0) { - GString *tb; - - tb = lua_touserdata (L, -1); - msg_err_config ("cannot call lua try_load_redis_servers script: %s", tb->str); - g_string_free (tb, TRUE); + msg_err_config ("cannot call lua try_load_redis_servers script: %s", + lua_tostring (L, -1)); lua_settop (L, 0); return FALSE; diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index 214fffca6..776baf3b9 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -348,8 +348,7 @@ gint rspamd_lua_traceback (lua_State *L); * @param L * @return */ -GString * -rspamd_lua_get_traceback_string (lua_State *L); +void rspamd_lua_get_traceback_string (lua_State *L, luaL_Buffer *buf); /** * Returns size of table at position `tbl_pos` diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index a60ad8493..cc0fa44ee 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -1130,7 +1130,6 @@ lua_metric_symbol_callback (struct rspamd_task *task, struct rspamd_task **ptask; gint level = lua_gettop (cd->L), nresults, err_idx, ret; lua_State *L = cd->L; - GString *tb; struct rspamd_symbol_result *s; cd->item = item; @@ -1152,13 +1151,9 @@ lua_metric_symbol_callback (struct rspamd_task *task, *ptask = task; if ((ret = lua_pcall (L, 1, LUA_MULTRET, err_idx)) != 0) { - tb = lua_touserdata (L, -1); - msg_err_task ("call to (%s) failed (%d): %v", cd->symbol, ret, tb); - - if (tb) { - g_string_free (tb, TRUE); - lua_pop (L, 1); - } + msg_err_task ("call to (%s) failed (%d): %s", cd->symbol, ret, + lua_tostring (L, -1)); + lua_settop (L, err_idx); /* Not -1 here, as err_func is popped below */ } else { nresults = lua_gettop (L) - level; @@ -1395,12 +1390,8 @@ rspamd_lua_squeeze_rule (lua_State *L, /* Now call for squeeze function */ if (lua_pcall (L, 2, 1, err_idx) != 0) { - GString *tb = lua_touserdata (L, -1); - msg_err_config ("call to squeeze_virtual failed: %v", tb); - - if (tb) { - g_string_free (tb, TRUE); - } + msg_err_config ("call to squeeze_virtual failed: %s", + lua_tostring (L, -1)); } ret = lua_tonumber (L, -1); @@ -1449,12 +1440,8 @@ rspamd_lua_squeeze_rule (lua_State *L, /* Now call for squeeze function */ if (lua_pcall (L, 3, 1, err_idx) != 0) { - GString *tb = lua_touserdata (L, -1); - msg_err_config ("call to squeeze_rule failed: %v", tb); - - if (tb) { - g_string_free (tb, TRUE); - } + msg_err_config ("call to squeeze_rule failed: %s", + lua_tostring (L, -1)); } ret = lua_tonumber (L, -1); @@ -2215,12 +2202,8 @@ rspamd_lua_squeeze_dependency (lua_State *L, struct rspamd_config *cfg, lua_pushstring (L, parent); if (lua_pcall (L, 2, 1, err_idx) != 0) { - GString *tb = lua_touserdata (L, -1); - msg_err_config ("call to squeeze_dependency script failed: %v", tb); - - if (tb) { - g_string_free (tb, TRUE); - } + msg_err_config ("call to squeeze_dependency script failed: %s", + lua_tostring (L, -1)); } else { ret = lua_toboolean (L, -1); @@ -3732,7 +3715,6 @@ lua_include_trace_cb (struct ucl_parser *parser, struct rspamd_lua_include_trace_cbdata *cbdata = (struct rspamd_lua_include_trace_cbdata *)user_data; gint err_idx; - GString *tb; lua_State *L; L = cbdata->L; @@ -3760,11 +3742,7 @@ lua_include_trace_cb (struct ucl_parser *parser, } if (lua_pcall (L, 4, 0, err_idx) != 0) { - tb = lua_touserdata (L, -1); - msg_err ("lua call to local include trace failed: %v", tb); - if (tb) { - g_string_free (tb, TRUE); - } + msg_err ("lua call to local include trace failed: %s", lua_tostring (L, -1)); } lua_settop (L, err_idx - 1); @@ -4014,7 +3992,6 @@ lua_config_register_re_selector (lua_State *L) } else { gint err_idx, ret; - GString *tb; struct rspamd_config **pcfg; lua_pushcfunction (L, &rspamd_lua_traceback); @@ -4030,13 +4007,9 @@ lua_config_register_re_selector (lua_State *L) lua_pushstring (L, delimiter); if ((ret = lua_pcall (L, 3, 1, err_idx)) != 0) { - tb = lua_touserdata (L, -1); msg_err_config ("call to create_selector_closure lua " - "script failed (%d): %v", ret, tb); - - if (tb) { - g_string_free (tb, TRUE); - } + "script failed (%d): %s", ret, + lua_tostring (L, -1)); } else { if (lua_type (L, -1) != LUA_TFUNCTION) { diff --git a/src/lua/lua_dns_resolver.c b/src/lua/lua_dns_resolver.c index 4234504ee..56b6989c0 100644 --- a/src/lua/lua_dns_resolver.c +++ b/src/lua/lua_dns_resolver.c @@ -143,7 +143,6 @@ lua_dns_resolver_callback (struct rdns_reply *reply, gpointer arg) struct lua_callback_state cbs; rspamd_mempool_t *pool; gint err_idx; - GString *tb = NULL; pool = cd->pool; lua_thread_pool_prepare_callback (cd->resolver->cfg->lua_thread_pool, &cbs); @@ -199,12 +198,8 @@ lua_dns_resolver_callback (struct rdns_reply *reply, gpointer arg) } if (lua_pcall (L, 7, 0, err_idx) != 0) { - tb = lua_touserdata (L, -1); - - if (tb) { - msg_err_pool_check ("call to dns callback failed: %s", tb->str); - g_string_free (tb, TRUE); - } + msg_err_pool_check ("call to dns callback failed: %s", + lua_tostring (L, -1)); } lua_settop (L, err_idx - 1); diff --git a/src/lua/lua_expression.c b/src/lua/lua_expression.c index 84d33a26e..c0e75fe84 100644 --- a/src/lua/lua_expression.c +++ b/src/lua/lua_expression.c @@ -216,9 +216,7 @@ lua_atom_process (gpointer runtime_ud, rspamd_expression_atom_t *atom) } if (lua_pcall (pd->L, nargs, 1, err_idx) != 0) { - GString *tb = lua_touserdata (pd->L, -1); - msg_info ("expression process callback failed: %s", tb->str); - g_string_free (tb, TRUE); + msg_info ("expression process callback failed: %s", lua_tostring (pd->L, -1)); } else { ret = lua_tonumber (pd->L, -1); diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index e58ff17c7..bfae06fc7 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -5685,10 +5685,8 @@ lua_lookup_words_array (lua_State *L, rspamd_lua_push_full_word (L, tok); if (lua_pcall (L, 1, 0, err_idx) != 0) { - GString *tb = lua_touserdata (L, -1); msg_err_task ("cannot call callback function for lookup words: %s", - tb->str); - g_string_free (tb, TRUE); + lua_tostring (L, -1)); } lua_settop (L, err_idx - 1); diff --git a/src/lua/lua_thread_pool.c b/src/lua/lua_thread_pool.c index 3d289a12e..1fa584c51 100644 --- a/src/lua/lua_thread_pool.c +++ b/src/lua/lua_thread_pool.c @@ -278,7 +278,6 @@ lua_resume_thread_internal_full (struct thread_entry *thread_entry, { gint ret; struct lua_thread_pool *pool; - GString *tb; struct rspamd_task *task; msg_debug_lua_threads ("%s: lua_resume_thread_internal_full", loc); @@ -297,6 +296,7 @@ lua_resume_thread_internal_full (struct thread_entry *thread_entry, else { pool = thread_entry->cfg->lua_thread_pool; } + if (ret == 0) { if (thread_entry->finish_callback) { thread_entry->finish_callback (thread_entry, ret); @@ -304,21 +304,21 @@ lua_resume_thread_internal_full (struct thread_entry *thread_entry, lua_thread_pool_return_full (pool, thread_entry, loc); } else { - tb = rspamd_lua_get_traceback_string (thread_entry->lua_state); - if (tb && thread_entry->error_callback) { - thread_entry->error_callback (thread_entry, ret, tb->str); + rspamd_lua_traceback (thread_entry->lua_state); + if (thread_entry->error_callback) { + thread_entry->error_callback (thread_entry, ret, + lua_tostring (thread_entry->lua_state, -1)); } else if (thread_entry->task) { task = thread_entry->task; - msg_err_task ("lua call failed (%d): %v", ret, tb); + msg_err_task ("lua call failed (%d): %s", ret, + lua_tostring (thread_entry->lua_state, -1)); } else { - msg_err ("lua call failed (%d): %v", ret, tb); + msg_err ("lua call failed (%d): %s", ret, + lua_tostring (thread_entry->lua_state, -1)); } - if (tb) { - g_string_free (tb, TRUE); - } /* * Maybe there is a way to recover here. * For now, just remove faulty thread diff --git a/src/lua/lua_upstream.c b/src/lua/lua_upstream.c index 37c541d9d..3c4f3f4c3 100644 --- a/src/lua/lua_upstream.c +++ b/src/lua/lua_upstream.c @@ -479,9 +479,7 @@ lua_upstream_watch_func (struct upstream *up, lua_pushinteger (L, cur_errors); if (lua_pcall (L, 3, 0, err_idx) != 0) { - GString *tb = lua_touserdata (L, -1); - msg_err ("cannot call watch function for upstream: %s", tb->str); - g_string_free (tb, TRUE); + msg_err ("cannot call watch function for upstream: %s", lua_tostring (L, -1)); lua_settop (L, 0); return; diff --git a/src/lua/lua_worker.c b/src/lua/lua_worker.c index 5c690ce8e..332d7e663 100644 --- a/src/lua/lua_worker.c +++ b/src/lua/lua_worker.c @@ -291,7 +291,6 @@ rspamd_lua_execute_lua_subprocess (lua_State *L, struct rspamd_lua_process_cbdata *cbdata) { gint err_idx, r; - GString *tb; guint64 wlen = 0; const gchar *ret; gsize retlen; @@ -302,23 +301,22 @@ rspamd_lua_execute_lua_subprocess (lua_State *L, lua_rawgeti (L, LUA_REGISTRYINDEX, cbdata->func_cbref); if (lua_pcall (L, 0, 1, err_idx) != 0) { - tb = lua_touserdata (L, -1); - msg_err ("call to subprocess failed: %v", tb); + const gchar *s = lua_tostring (L, -1); + gsize slen = strlen (s); + + msg_err ("call to subprocess failed: %s", s); /* Indicate error */ - wlen = (1ULL << 63) + tb->len; + wlen = (1ULL << 63u) + slen; r = write (cbdata->sp[1], &wlen, sizeof (wlen)); if (r == -1) { msg_err ("write failed: %s", strerror (errno)); } - r = write (cbdata->sp[1], tb->str, tb->len); + r = write (cbdata->sp[1], s, slen); if (r == -1) { msg_err ("write failed: %s", strerror (errno)); } - g_string_free (tb, TRUE); - - lua_pop (L, 1); } else { ret = lua_tolstring (L, -1, &retlen); @@ -335,7 +333,7 @@ rspamd_lua_execute_lua_subprocess (lua_State *L, } } - lua_pop (L, 1); /* Error function */ + lua_settop (L, err_idx - 1); } static void @@ -345,7 +343,6 @@ rspamd_lua_call_on_complete (lua_State *L, const gchar *data, gsize datalen) { gint err_idx; - GString *tb; lua_pushcfunction (L, &rspamd_lua_traceback); err_idx = lua_gettop (L); @@ -367,12 +364,11 @@ rspamd_lua_call_on_complete (lua_State *L, } if (lua_pcall (L, 2, 0, err_idx) != 0) { - tb = lua_touserdata (L, -1); - msg_err ("call to subprocess callback script failed: %v", tb); - lua_pop (L, 1); + msg_err ("call to on_complete script failed: %s", + lua_tostring (L, -1)); } - lua_pop (L, 1); /* Error function */ + lua_settop (L, err_idx - 1); } static gboolean diff --git a/src/plugins/dkim_check.c b/src/plugins/dkim_check.c index 0cfea3f92..7a242934f 100644 --- a/src/plugins/dkim_check.c +++ b/src/plugins/dkim_check.c @@ -1317,7 +1317,7 @@ dkim_sign_callback (struct rspamd_task *task, gint err_idx; gint64 arc_idx = 0; gsize len; - GString *tb, *hdr; + GString *hdr; GList *sigs = NULL; GError *err = NULL; const gchar *selector = NULL, *domain = NULL, *key = NULL, *key_type = NULL, @@ -1344,9 +1344,8 @@ dkim_sign_callback (struct rspamd_task *task, rspamd_lua_setclass (L, "rspamd{task}", -1); if (lua_pcall (L, 1, 1, err_idx) != 0) { - tb = lua_touserdata (L, -1); - msg_err_task ("call to user extraction script failed: %v", tb); - g_string_free (tb, TRUE); + msg_err_task ("call to user extraction script failed: %s", + lua_tostring (L, -1)); } else { if (lua_istable (L, -1)) { diff --git a/src/plugins/fuzzy_check.c b/src/plugins/fuzzy_check.c index 311bd3945..639b0edf8 100644 --- a/src/plugins/fuzzy_check.c +++ b/src/plugins/fuzzy_check.c @@ -583,7 +583,6 @@ fuzzy_parse_rule (struct rspamd_config *cfg, const ucl_object_t *obj, * Process rule in Lua */ gint err_idx, ret; - GString *tb; lua_State *L = (lua_State *)cfg->lua_state; lua_pushcfunction (L, &rspamd_lua_traceback); @@ -592,13 +591,9 @@ fuzzy_parse_rule (struct rspamd_config *cfg, const ucl_object_t *obj, ucl_object_push_lua (L, obj, true); if ((ret = lua_pcall (L, 1, 1, err_idx)) != 0) { - tb = lua_touserdata (L, -1); msg_err_config ("call to process_rule lua " - "script failed (%d): %v", ret, tb); + "script failed (%d): %s", ret, lua_tostring (L, -1)); - if (tb) { - g_string_free (tb, TRUE); - } rule->lua_id = -1; } else { @@ -1147,7 +1142,6 @@ fuzzy_check_module_reconfig (struct rspamd_config *cfg) if (fuzzy_module_ctx->cleanup_rules_ref != -1) { /* Sync lua_fuzzy rules */ gint err_idx, ret; - GString *tb; lua_State *L = (lua_State *)cfg->lua_state; lua_pushcfunction (L, &rspamd_lua_traceback); @@ -1155,13 +1149,8 @@ fuzzy_check_module_reconfig (struct rspamd_config *cfg) lua_rawgeti (L, LUA_REGISTRYINDEX, fuzzy_module_ctx->cleanup_rules_ref); if ((ret = lua_pcall (L, 0, 0, err_idx)) != 0) { - tb = lua_touserdata (L, -1); msg_err_config ("call to cleanup_rules lua " - "script failed (%d): %v", ret, tb); - - if (tb) { - g_string_free (tb, TRUE); - } + "script failed (%d): %s", ret, lua_tostring (L, -1)); } luaL_unref (cfg->lua_state, LUA_REGISTRYINDEX, @@ -1381,7 +1370,6 @@ fuzzy_rule_check_mimepart (struct rspamd_task *task, { if (rule->lua_id != -1 && rule->ctx->check_mime_part_ref != -1) { gint err_idx, ret; - GString *tb; lua_State *L = (lua_State *)task->cfg->lua_state; struct rspamd_task **ptask; struct rspamd_mime_part **ppart; @@ -1401,13 +1389,9 @@ fuzzy_rule_check_mimepart (struct rspamd_task *task, lua_pushnumber (L, rule->lua_id); if ((ret = lua_pcall (L, 3, 2, err_idx)) != 0) { - tb = lua_touserdata (L, -1); msg_err_task ("call to check_mime_part lua " - "script failed (%d): %v", ret, tb); + "script failed (%d): %s", ret, lua_tostring (L, -1)); - if (tb) { - g_string_free (tb, TRUE); - } ret = FALSE; } else { @@ -2956,7 +2940,6 @@ fuzzy_process_handler (struct rspamd_http_connection_entry *conn_ent, guint i; GError **err; GPtrArray *commands; - GString *tb; lua_State *L; gint r, *saved, rules = 0, err_idx; struct fuzzy_ctx *fuzzy_module_ctx; @@ -3016,9 +2999,8 @@ fuzzy_process_handler (struct rspamd_http_connection_entry *conn_ent, rspamd_lua_setclass (L, "rspamd{task}", -1); if (lua_pcall (L, 1, LUA_MULTRET, err_idx) != 0) { - tb = lua_touserdata (L, -1); - msg_err_task ("call to user extraction script failed: %v", tb); - g_string_free (tb, TRUE); + msg_err_task ("call to fuzzy learn condition failed: %s", + lua_tostring (L, -1)); } else { if (lua_gettop (L) > err_idx + 1) { diff --git a/src/plugins/surbl.c b/src/plugins/surbl.c index 3b33e3e7f..444914591 100644 --- a/src/plugins/surbl.c +++ b/src/plugins/surbl.c @@ -972,9 +972,7 @@ surbl_module_parse_rule (const ucl_object_t* value, struct rspamd_config* cfg) if (loaded) { if (lua_pcall (L, 0, 1, err_idx) != 0) { - tb = lua_touserdata (L, -1); - msg_err_config ("call failed: %v\n", tb); - g_string_free (tb, TRUE); + msg_err_config ("call failed: %s\n", lua_tostring (L, -1)); } else if (lua_isfunction (L, -1)) { new_suffix->url_process_cbref = luaL_ref (L, diff --git a/src/rspamd_proxy.c b/src/rspamd_proxy.c index db654c0b0..deba18dab 100644 --- a/src/rspamd_proxy.c +++ b/src/rspamd_proxy.c @@ -224,7 +224,6 @@ rspamd_proxy_parse_lua_parser (lua_State *L, const ucl_object_t *obj, const gchar *lua_script; gsize slen; gint err_idx, ref_idx; - GString *tb = NULL; gboolean has_ref = FALSE; g_assert (obj != NULL); @@ -252,13 +251,11 @@ rspamd_proxy_parse_lua_parser (lua_State *L, const ucl_object_t *obj, /* Now do it */ if (lua_pcall (L, 0, 1, err_idx) != 0) { - tb = lua_touserdata (L, -1); g_set_error (err, rspamd_proxy_quark (), EINVAL, "cannot init lua parser script: %s", - tb->str); - g_string_free (tb, TRUE); + lua_tostring (L, -1)); lua_settop (L, 0); return FALSE; @@ -644,7 +641,6 @@ rspamd_proxy_parse_script (rspamd_mempool_t *pool, const gchar *lua_script; gsize slen; gint err_idx, ref_idx; - GString *tb = NULL; struct stat st; ctx = pd->user_struct; @@ -690,13 +686,11 @@ rspamd_proxy_parse_script (rspamd_mempool_t *pool, /* Now do it */ if (lua_pcall (L, 0, 1, err_idx) != 0) { - tb = lua_touserdata (L, -1); g_set_error (err, rspamd_proxy_quark (), EINVAL, "cannot init lua parser script: %s", - tb->str); - g_string_free (tb, TRUE); + lua_tostring (L, -1)); lua_settop (L, 0); goto err; @@ -872,7 +866,6 @@ proxy_backend_parse_results (struct rspamd_proxy_session *session, const gchar *in, gsize inlen) { struct ucl_parser *parser; - GString *tb = NULL; gint err_idx; if (inlen == 0 || in == NULL) { @@ -889,11 +882,9 @@ proxy_backend_parse_results (struct rspamd_proxy_session *session, lua_pushlstring (L, in, inlen); if (lua_pcall (L, 1, 1, err_idx) != 0) { - tb = lua_touserdata (L, -1); msg_err_session ( "cannot run lua parser script: %s", - tb->str); - g_string_free (tb, TRUE); + lua_tostring (L, -1)); lua_settop (L, 0); return FALSE; @@ -928,7 +919,6 @@ proxy_backend_parse_results (struct rspamd_proxy_session *session, static void proxy_call_cmp_script (struct rspamd_proxy_session *session, gint cbref) { - GString *tb = NULL; gint err_idx; guint i; struct rspamd_proxy_backend_connection *conn; @@ -969,11 +959,9 @@ proxy_call_cmp_script (struct rspamd_proxy_session *session, gint cbref) } if (lua_pcall (L, 1, 0, err_idx) != 0) { - tb = lua_touserdata (L, -1); msg_err_session ( "cannot run lua compare script: %s", - tb->str); - g_string_free (tb, TRUE); + lua_tostring (L, -1)); } lua_settop (L, 0); -- 2.39.5