From: Vsevolod Stakhov Date: Wed, 29 Jan 2020 15:03:08 +0000 (+0000) Subject: [Rework] Rework options hash structure X-Git-Tag: 2.3~26 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=971025ceca5af31e11a1d079302365bb6f3d7f8c;p=rspamd.git [Rework] Rework options hash structure --- diff --git a/src/libmime/scan_result.c b/src/libmime/scan_result.c index 7a8451f9c..671e99a9b 100644 --- a/src/libmime/scan_result.c +++ b/src/libmime/scan_result.c @@ -293,7 +293,7 @@ insert_metric_result (struct rspamd_task *task, s->nshots ++; if (opt) { - rspamd_task_add_result_option (task, s, opt); + rspamd_task_add_result_option (task, s, opt, strlen (opt)); } /* Adjust diff */ @@ -459,7 +459,9 @@ insert_metric_result (struct rspamd_task *task, s->score = 0; } - rspamd_task_add_result_option (task, s, opt); + if (opt) { + rspamd_task_add_result_option (task, s, opt, strlen (opt)); + } } msg_debug_metric ("final insertion for symbol %s, score %.2f, factor: %f", @@ -503,12 +505,13 @@ rspamd_task_insert_result_full (struct rspamd_task *task, gboolean rspamd_task_add_result_option (struct rspamd_task *task, - struct rspamd_symbol_result *s, const gchar *val) + struct rspamd_symbol_result *s, + const gchar *val, + gsize vlen) { - struct rspamd_symbol_option *opt; + struct rspamd_symbol_option *opt, srch; gboolean ret = FALSE; gchar *opt_cpy = NULL; - gsize vlen; khiter_t k; gint r; @@ -524,8 +527,6 @@ rspamd_task_add_result_option (struct rspamd_task *task, s->options = kh_init (rspamd_options_hash); } - vlen = strlen (val); - 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", @@ -544,7 +545,9 @@ rspamd_task_add_result_option (struct rspamd_task *task, if (!(s->sym && (s->sym->flags & RSPAMD_SYMBOL_FLAG_ONEPARAM)) && kh_size (s->options) < task->cfg->default_max_shots) { /* Append new options */ - k = kh_get (rspamd_options_hash, s->options, val); + srch.option = (gchar *)val; + srch.optlen = vlen; + k = kh_get (rspamd_options_hash, s->options, &srch); if (k == kh_end (s->options)) { opt = rspamd_mempool_alloc0 (task->task_pool, sizeof (*opt)); @@ -553,9 +556,10 @@ rspamd_task_add_result_option (struct rspamd_task *task, opt_cpy = rspamd_mempool_strdup (task->task_pool, val); } - k = kh_put (rspamd_options_hash, s->options, opt_cpy, &r); + opt->optlen = vlen; + opt->option = opt_cpy; - kh_value (s->options, k) = opt; + k = kh_put (rspamd_options_hash, s->options, opt, &r); opt->option = opt_cpy; DL_APPEND (s->opts_head, opt); diff --git a/src/libmime/scan_result.h b/src/libmime/scan_result.h index 3b222fffb..74d0d81b9 100644 --- a/src/libmime/scan_result.h +++ b/src/libmime/scan_result.h @@ -20,6 +20,7 @@ struct rspamd_classifier_config; struct rspamd_symbol_option { gchar *option; + gsize optlen; struct rspamd_symbol_option *prev, *next; }; @@ -142,7 +143,9 @@ struct rspamd_symbol_result *rspamd_task_insert_result_full (struct rspamd_task * @param opt */ gboolean rspamd_task_add_result_option (struct rspamd_task *task, - struct rspamd_symbol_result *s, const gchar *opt); + struct rspamd_symbol_result *s, + const gchar *opt, + gsize vlen); /** * Finds symbol result diff --git a/src/libmime/scan_result_private.h b/src/libmime/scan_result_private.h index aa90430e1..cb4ff4cda 100644 --- a/src/libmime/scan_result_private.h +++ b/src/libmime/scan_result_private.h @@ -12,7 +12,23 @@ extern "C" { #endif -KHASH_MAP_INIT_STR (rspamd_options_hash, struct rspamd_symbol_option *); +#define RSPAMD_OPTS_SEED 0x9f1f608628a4fefbULL +#define rspamd_symopt_hash(opt) (rspamd_cryptobox_fast_hash ( \ + ((struct rspamd_symbol_option *)opt)->option, \ + ((struct rspamd_symbol_option *)opt)->optlen, RSPAMD_OPTS_SEED)) +static inline bool +rspamd_symopt_equal (const struct rspamd_symbol_option *o1, + const struct rspamd_symbol_option *o2) +{ + if (o1->optlen == o2->optlen) { + return (memcmp (o1->option, o2->option, o1->optlen) == 0); + } + + return false; +} + +KHASH_INIT (rspamd_options_hash, struct rspamd_symbol_option *, char, + 0, rspamd_symopt_hash, rspamd_symopt_equal); /** * Result of metric processing */