commit 48db96c: [Minor] Fix various issues

Vsevolod Stakhov vsevolod at highsecure.ru
Tue Jan 15 12:56:08 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-01-15 12:50:35 +0000
URL: https://github.com/rspamd/rspamd/commit/48db96cdcb93d2495dc3b550056aa9bdfbbd0a60

[Minor] Fix various issues

---
 src/libmime/mime_expressions.c | 57 +++++++++++++++++++++++++-----------------
 src/lua/lua_html.c             |  1 +
 src/plugins/regexp.c           |  2 +-
 3 files changed, 36 insertions(+), 24 deletions(-)

diff --git a/src/libmime/mime_expressions.c b/src/libmime/mime_expressions.c
index 6657570e7..bdc6e46f5 100644
--- a/src/libmime/mime_expressions.c
+++ b/src/libmime/mime_expressions.c
@@ -640,7 +640,7 @@ rspamd_mime_expr_parse (const gchar *line, gsize len,
 {
 	rspamd_expression_atom_t *a = NULL;
 	struct rspamd_mime_atom *mime_atom = NULL;
-	const gchar *p, *end;
+	const gchar *p, *end, *c = NULL;
 	struct rspamd_mime_expr_ud *real_ud = (struct rspamd_mime_expr_ud *)ud;
 	struct rspamd_config *cfg;
 	rspamd_regexp_t *own_re;
@@ -656,6 +656,7 @@ rspamd_mime_expr_parse (const gchar *line, gsize len,
 		in_flags_brace,
 		got_obrace,
 		in_function,
+		in_local_function,
 		got_ebrace,
 		end_atom,
 		bad_atom
@@ -682,9 +683,8 @@ rspamd_mime_expr_parse (const gchar *line, gsize len,
 				if (t == ':') {
 					if (p - line == 3 && memcmp (line, "lua", 3) == 0) {
 						type = MIME_ATOM_LOCAL_LUA_FUNCTION;
-						state = end_atom;
-						p ++;
-						continue;
+						state = in_local_function;
+						c = p + 1;
 					}
 				}
 				else {
@@ -759,6 +759,15 @@ rspamd_mime_expr_parse (const gchar *line, gsize len,
 			}
 			p ++;
 			break;
+		case in_local_function:
+			if (!(g_ascii_isalnum (t) || t == '-' || t == '_')) {
+				g_assert (c != NULL);
+				state = end_atom;
+			}
+			else {
+				p++;
+			}
+			break;
 		case got_ebrace:
 			state = end_atom;
 			break;
@@ -783,15 +792,8 @@ set:
 
 	mime_atom = rspamd_mempool_alloc (pool, sizeof (*mime_atom));
 	mime_atom->type = type;
-
-	if (type != MIME_ATOM_LOCAL_LUA_FUNCTION) {
-		mime_atom->str = rspamd_mempool_alloc (pool, p - line + 1);
-		rspamd_strlcpy (mime_atom->str, line, p - line + 1);
-	}
-	else {
-		mime_atom->str = rspamd_mempool_alloc (pool, end - p + 1);
-		rspamd_strlcpy (mime_atom->str, p, end - p + 1);
-	}
+	mime_atom->str = rspamd_mempool_alloc (pool, p - line + 1);
+	rspamd_strlcpy (mime_atom->str, line, p - line + 1);
 
 	if (type == MIME_ATOM_REGEXP) {
 		mime_atom->d.re = rspamd_mime_expr_parse_regexp_atom (pool,
@@ -905,20 +907,20 @@ set:
 
 		const ucl_object_t *function_obj;
 
-		function_obj = ucl_object_lookup_len (functions, p,
-				end - p);
+		function_obj = ucl_object_lookup_len (functions, c,
+				p - c);
 
 		if (function_obj == NULL) {
 			g_set_error (err, rspamd_mime_expr_quark(), 320,
 					"function %*.s is not found for '%s'",
-					(int)(end - p), p, mime_atom->str);
+					(int)(p - c), c, mime_atom->str);
 			goto err;
 		}
 
 		if (ucl_object_type (function_obj) != UCL_USERDATA) {
 			g_set_error (err, rspamd_mime_expr_quark(), 320,
 					"function %*.s has invalid type for '%s'",
-					(int)(end - p), p, mime_atom->str);
+					(int)(p - c), c, mime_atom->str);
 			goto err;
 		}
 
@@ -1111,15 +1113,24 @@ rspamd_mime_expr_process (struct rspamd_expr_process_data *process_data, rspamd_
 		}
 	}
 	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);
+		err_idx = lua_gettop (L);
+
 		lua_rawgeti (L, LUA_REGISTRYINDEX, mime_atom->d.lua_cbref);
 		rspamd_lua_task_push (L, task);
 
-		if (lua_pcall (L, 1, 1, 0) != 0) {
-			msg_info_task ("lua call to local function for atom '%s' failed: %s",
+		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",
 					mime_atom->str,
-					lua_tostring (L, -1));
-			lua_pop (L, 1);
+					tb);
+			if (tb) {
+				g_string_free (tb, TRUE);
+			}
 		}
 		else {
 			if (lua_type (L, -1) == LUA_TBOOLEAN) {
@@ -1132,9 +1143,9 @@ rspamd_mime_expr_process (struct rspamd_expr_process_data *process_data, rspamd_
 				msg_err_task ("%s returned wrong return type: %s",
 						mime_atom->str, lua_typename (L, lua_type (L, -1)));
 			}
-			/* Remove result */
-			lua_pop (L, 1);
 		}
+
+		lua_settop (L, 0);
 	}
 	else {
 		ret = rspamd_mime_expr_process_function (mime_atom->d.func, task,
diff --git a/src/lua/lua_html.c b/src/lua/lua_html.c
index 71578e1a4..739776b01 100644
--- a/src/lua/lua_html.c
+++ b/src/lua/lua_html.c
@@ -315,6 +315,7 @@ lua_html_push_image (lua_State *L, struct html_image *img)
 	lua_settable (L, -3);
 	lua_pushstring (L, "embedded");
 	lua_pushboolean (L, img->flags & RSPAMD_HTML_FLAG_IMAGE_EMBEDDED);
+	lua_settable (L, -3);
 	lua_pushstring (L, "data");
 	lua_pushboolean (L, img->flags & RSPAMD_HTML_FLAG_IMAGE_DATA);
 	lua_settable (L, -3);
diff --git a/src/plugins/regexp.c b/src/plugins/regexp.c
index 87ab1f9c0..07d8e81b0 100644
--- a/src/plugins/regexp.c
+++ b/src/plugins/regexp.c
@@ -223,7 +223,7 @@ regexp_module_config (struct rspamd_config *cfg)
 					cur_item->symbol = ucl_object_key (value);
 					cur_item->magic = rspamd_regexp_cb_magic;
 					ud.cfg = cfg;
-					ud.conf_obj = elt;
+					ud.conf_obj = value;
 
 					if (!read_regexp_expression (cfg->cfg_pool,
 							cur_item, ucl_object_key (value),


More information about the Commits mailing list