commit 095fb46: [Minor] Fix options insertion when a shadow result is presented

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Jan 28 20:49:06 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-01-28 20:45:08 +0000
URL: https://github.com/rspamd/rspamd/commit/095fb4617c45c64a17d75d888aca2cc1d03548ae (HEAD -> master)

[Minor] Fix options insertion when a shadow result is presented

---
 src/libmime/scan_result.c | 93 ++++++++++++++++++++++++++++-------------------
 src/libmime/scan_result.h |  1 +
 2 files changed, 56 insertions(+), 38 deletions(-)

diff --git a/src/libmime/scan_result.c b/src/libmime/scan_result.c
index 1a2d96d83..63e1de298 100644
--- a/src/libmime/scan_result.c
+++ b/src/libmime/scan_result.c
@@ -530,6 +530,7 @@ rspamd_task_insert_result_full (struct rspamd_task *task,
 				}
 			}
 
+			s =  NULL;
 			s = insert_metric_result (task,
 					symbol,
 					weight,
@@ -547,6 +548,10 @@ rspamd_task_insert_result_full (struct rspamd_task *task,
 							s->sym->cache_item);
 				}
 			}
+			else {
+				/* O(N) but we normally don't have any shadow results */
+				LL_APPEND (ret, s);
+			}
 		}
 	}
 	else {
@@ -679,54 +684,66 @@ rspamd_task_add_result_option (struct rspamd_task *task,
 	gsize cpy_len;
 	khiter_t k;
 	gint r;
+	struct rspamd_symbol_result *cur;
 
 	if (s && val) {
-		if (s->opts_len < 0) {
-			/* Cannot add more options, give up */
-			msg_debug_task ("cannot add more options to symbol %s when adding option %s",
-					s->name, val);
-			return FALSE;
-		}
+		/*
+		 * Here we assume that this function is all the time called with the
+		 * symbol from the default result, not some shadow result, or
+		 * the option insertion will be wrong
+		 */
+		LL_FOREACH (s, cur) {
+			if (cur->opts_len < 0) {
+				/* Cannot add more options, give up */
+				msg_debug_task ("cannot add more options to symbol %s when adding option %s",
+						cur->name, val);
+				continue;
+			}
 
-		if (!s->options) {
-			s->options = kh_init (rspamd_options_hash);
-		}
+			if (!cur->options) {
+				cur->options = kh_init (rspamd_options_hash);
+			}
 
-		if (vlen + s->opts_len > task->cfg->max_opts_len) {
-			/* Add truncated option */
-			msg_info_task ("cannot add more options to symbol %s when adding option %s",
-					s->name, val);
-			val = "...";
-			vlen = 3;
-			s->opts_len = -1;
-		}
+			if (vlen + cur->opts_len > task->cfg->max_opts_len) {
+				/* Add truncated option */
+				msg_info_task ("cannot add more options to symbol %s when adding option %s",
+						cur->name, val);
+				val = "...";
+				vlen = 3;
+				cur->opts_len = -1;
+			}
 
-		if (!(s->sym && (s->sym->flags & RSPAMD_SYMBOL_FLAG_ONEPARAM)) &&
-				kh_size (s->options) < task->cfg->default_max_shots) {
-			opt_cpy = rspamd_task_option_safe_copy (task, val, vlen, &cpy_len);
-			/* Append new options */
-			srch.option = (gchar *)opt_cpy;
-			srch.optlen = cpy_len;
-			k = kh_get (rspamd_options_hash, s->options, &srch);
+			if (!(cur->sym && (cur->sym->flags & RSPAMD_SYMBOL_FLAG_ONEPARAM)) &&
+				kh_size (cur->options) < task->cfg->default_max_shots) {
+				opt_cpy = rspamd_task_option_safe_copy (task, val, vlen, &cpy_len);
+				/* Append new options */
+				srch.option = (gchar *) opt_cpy;
+				srch.optlen = cpy_len;
+				k = kh_get (rspamd_options_hash, cur->options, &srch);
 
-			if (k == kh_end (s->options)) {
-				opt = rspamd_mempool_alloc0 (task->task_pool, sizeof (*opt));
-				opt->optlen = cpy_len;
-				opt->option = opt_cpy;
+				if (k == kh_end (cur->options)) {
+					opt = rspamd_mempool_alloc0 (task->task_pool, sizeof (*opt));
+					opt->optlen = cpy_len;
+					opt->option = opt_cpy;
 
-				kh_put (rspamd_options_hash, s->options, opt, &r);
-				DL_APPEND (s->opts_head, opt);
+					kh_put (rspamd_options_hash, cur->options, opt, &r);
+					DL_APPEND (cur->opts_head, opt);
 
-				ret = TRUE;
+					if (s == cur) {
+						ret = TRUE;
+					}
+				}
+			}
+			else {
+				/* Skip addition */
+				if (s == cur) {
+					ret = FALSE;
+				}
 			}
-		}
-		else {
-			/* Skip addition */
-			ret = FALSE;
-		}
 
-		if (ret && s->opts_len >= 0) {
-			s->opts_len += vlen;
+			if (ret && cur->opts_len >= 0) {
+				cur->opts_len += vlen;
+			}
 		}
 	}
 	else if (!val) {
diff --git a/src/libmime/scan_result.h b/src/libmime/scan_result.h
index c2b1088da..b7a548be0 100644
--- a/src/libmime/scan_result.h
+++ b/src/libmime/scan_result.h
@@ -43,6 +43,7 @@ struct rspamd_symbol_result {
 	gssize opts_len;                               /**< total size of all options (negative if truncated option is added) */
 	guint nshots;
 	enum rspamd_symbol_result_flags flags;
+	struct rspamd_symbol_result *next;
 };
 
 


More information about the Commits mailing list