]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Project] Adopt functions
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 24 Jul 2018 18:01:40 +0000 (19:01 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 24 Jul 2018 18:01:40 +0000 (19:01 +0100)
contrib/libucl/khash.h
src/libmime/filter.c
src/libmime/filter.h
src/libserver/composites.c
src/libserver/protocol.c
src/libserver/roll_history.c
src/libserver/task.c
src/libstat/stat_process.c
src/lua/lua_task.c

index afc3ce3ef0dcbe6a5388aaa6bdf7f0f87af75e6c..a91db5b2a528e797b33968d734052d21b09b9ea1 100644 (file)
@@ -576,6 +576,13 @@ static kh_inline khint_t __ac_Wang_hash(khint_t key)
                code;                                                                                           \
        } }
 
+#define kh_foreach_value_ptr(h, pvvar, code) { khint_t __i;            \
+       for (__i = kh_begin(h); __i != kh_end(h); ++__i) {              \
+               if (!kh_exist(h,__i)) continue;                                         \
+               (pvvar) = &kh_val(h,__i);                                                       \
+               code;                                                                                           \
+       } }
+
 /* More conenient interfaces */
 
 /*! @function
index 8677ef8a1c98739147618cb252b7a8041309b4d2..e4862b0fd43bf14e3f6c7c5ada36b0957ae958b9 100644 (file)
@@ -106,6 +106,7 @@ insert_metric_result (struct rspamd_task *task,
        guint i;
        khiter_t k;
        gboolean single = !!(flags & RSPAMD_SYMBOL_INSERT_SINGLE);
+       gchar *sym_cpy;
 
        metric_res = rspamd_create_metric_result (task);
 
@@ -236,8 +237,9 @@ insert_metric_result (struct rspamd_task *task,
                }
        }
        else {
+               sym_cpy = rspamd_mempool_strdup (task->task_pool, symbol);
                k = kh_put (rspamd_symbols_hash, metric_res->symbols,
-                               symbol, &ret);
+                               sym_cpy, &ret);
                s = &kh_value (metric_res->symbols, k);
                memset (s, 0, sizeof (*s));
 
@@ -250,7 +252,7 @@ insert_metric_result (struct rspamd_task *task,
                        next_gf = task->cfg->grow_factor;
                }
 
-               s->name = symbol;
+               s->name = sym_cpy;
                s->sym = sdef;
                s->nshots = 1;
 
@@ -318,6 +320,7 @@ rspamd_task_add_result_option (struct rspamd_task *task,
 {
        struct rspamd_symbol_option *opt;
        gboolean ret = FALSE;
+       gchar *opt_cpy;
        khiter_t k;
        gint r;
 
@@ -329,10 +332,12 @@ rspamd_task_add_result_option (struct rspamd_task *task,
                        k = kh_get (rspamd_options_hash, s->options, val);
 
                        if (k == kh_end (s->options)) {
-                               k = kh_put (rspamd_options_hash, s->options, val, &r);
+                               opt = rspamd_mempool_alloc0 (task->task_pool, sizeof (*opt));
+                               opt_cpy = rspamd_mempool_strdup (task->task_pool, val);
+                               k = kh_put (rspamd_options_hash, s->options, opt_cpy, &r);
 
-                               opt = &kh_value (s->options, k);
-                               opt->option = rspamd_mempool_strdup (task->task_pool, val);
+                               kh_value (s->options, k) = opt;
+                               opt->option = opt_cpy;
                                DL_APPEND (s->opts_head, opt);
 
                                ret = TRUE;
@@ -340,10 +345,12 @@ rspamd_task_add_result_option (struct rspamd_task *task,
                }
                else {
                        s->options = kh_init (rspamd_options_hash);
-                       k = kh_put (rspamd_options_hash, s->options, val, &r);
+                       opt = rspamd_mempool_alloc0 (task->task_pool, sizeof (*opt));
+                       opt_cpy = rspamd_mempool_strdup (task->task_pool, val);
+                       k = kh_put (rspamd_options_hash, s->options, opt_cpy, &r);
 
-                       opt = &kh_value (s->options, k);
-                       opt->option = rspamd_mempool_strdup (task->task_pool, val);
+                       kh_value (s->options, k) = opt;
+                       opt->option = opt_cpy;
                        DL_APPEND (s->opts_head, opt);
 
                        ret = TRUE;
index de8cb57b8a156ab2621b9f305a6a9d72fc0166be..b6d7a435900b74df4925829801f462cf4ff44f3a 100644 (file)
@@ -30,7 +30,7 @@ enum rspamd_symbol_result_flags {
  */
 KHASH_INIT (rspamd_options_hash,
                const char *,
-               struct rspamd_symbol_option,
+               struct rspamd_symbol_option *,
                true,
                rspamd_str_hash,
                rspamd_str_equal);
index f7c31bb2013fd44ea23aa029e82e52fca29cbfd7..88dc51bd2935817545bf35ca8d7c10a9139b7695 100644 (file)
@@ -120,7 +120,7 @@ rspamd_composite_process_single_symbol (struct composites_data *cd,
        struct rspamd_composite *ncomp;
        struct rspamd_task *task = cd->task;
 
-       if ((ms = g_hash_table_lookup (cd->metric_res->symbols, sym)) == NULL) {
+       if ((ms = rspamd_task_find_symbol_result (cd->task, sym)) == NULL) {
                msg_debug_composites ("not found symbol %s in composite %s", sym,
                                cd->composite->sym);
                if ((ncomp =
@@ -144,14 +144,14 @@ rspamd_composite_process_single_symbol (struct composites_data *cd,
                                cd->composite = saved;
                                clrbit (cd->checked, cd->composite->id * 2);
 
-                               ms = g_hash_table_lookup (cd->metric_res->symbols, sym);
+                               ms = rspamd_task_find_symbol_result (cd->task, sym);
                        }
                        else {
                                /*
                                 * XXX: in case of cyclic references this would return 0
                                 */
                                if (isset (cd->checked, ncomp->id * 2 + 1)) {
-                                       ms = g_hash_table_lookup (cd->metric_res->symbols, sym);
+                                       ms = rspamd_task_find_symbol_result (cd->task, sym);
                                }
                        }
                }
@@ -190,7 +190,7 @@ rspamd_composite_expr_process (gpointer input, rspamd_expression_atom_t *atom)
        if (isset (cd->checked, cd->composite->id * 2)) {
                /* We have already checked this composite, so just return its value */
                if (isset (cd->checked, cd->composite->id * 2 + 1)) {
-                       ms = g_hash_table_lookup (cd->metric_res->symbols, sym);
+                       ms = rspamd_task_find_symbol_result (cd->task, sym);
                }
 
                if (ms) {
@@ -334,7 +334,7 @@ composites_foreach_callback (gpointer key, gpointer value, void *data)
                        clrbit (cd->checked, comp->id * 2 + 1);
                }
                else {
-                       if (g_hash_table_lookup (cd->metric_res->symbols, key) != NULL) {
+                       if (rspamd_task_find_symbol_result (cd->task, key) != NULL) {
                                /* Already set, no need to check */
                                msg_debug_composites ("composite %s is already in metric "
                                                "in composites bitfield", cd->composite->sym);
index 0e4f9cb0c9fc7cecd5c4bc19a86518923f45c3ff..c83451058fa927d8e9bbe1016f5f88793def0e47 100644 (file)
@@ -912,12 +912,10 @@ static ucl_object_t *
 rspamd_metric_result_ucl (struct rspamd_task *task,
        struct rspamd_metric_result *mres, ucl_object_t *top)
 {
-       GHashTableIter hiter;
        struct rspamd_symbol_result *sym;
        gboolean is_spam;
        enum rspamd_action_type action = METRIC_ACTION_NOACTION;
        ucl_object_t *obj = NULL, *sobj;
-       gpointer h, v;
        const gchar *subject;
 
        action = rspamd_check_action_metric (task, mres);
@@ -966,15 +964,12 @@ rspamd_metric_result_ucl (struct rspamd_task *task,
                obj = ucl_object_typed_new (UCL_OBJECT);
        }
 
-       g_hash_table_iter_init (&hiter, mres->symbols);
-
-       while (g_hash_table_iter_next (&hiter, &h, &v)) {
-               sym = (struct rspamd_symbol_result *)v;
+       kh_foreach_value_ptr (mres->symbols, sym, {
                if (!(sym->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) {
                        sobj = rspamd_metric_symbol_ucl (task, sym);
-                       ucl_object_insert_key (obj, sobj, h, 0, false);
+                       ucl_object_insert_key (obj, sobj, sym->name, 0, false);
                }
-       }
+       });
 
        if (task->cmd == CMD_CHECK_V2) {
                ucl_object_insert_key (top, obj, "symbols", 0, false);
@@ -1405,8 +1400,6 @@ rspamd_protocol_write_log_pipe (struct rspamd_task *task)
        struct rspamd_protocol_log_message_sum *ls;
        lua_State *L = task->cfg->lua_state;
        struct rspamd_metric_result *mres;
-       GHashTableIter it;
-       gpointer k, v;
        struct rspamd_symbol_result *sym;
        gint id, i;
        guint32 *sid, n = 0, nextra = 0;
@@ -1551,7 +1544,7 @@ rspamd_protocol_write_log_pipe (struct rspamd_task *task)
                                mres = task->result;
 
                                if (mres) {
-                                       n = g_hash_table_size (mres->symbols);
+                                       n = kh_size (mres->symbols);
                                        sz = sizeof (*ls) +
                                                        sizeof (struct rspamd_protocol_log_symbol_result) *
                                                        (n + nextra);
@@ -1574,13 +1567,11 @@ rspamd_protocol_write_log_pipe (struct rspamd_task *task)
                                        ls->nresults = n;
                                        ls->nextra = nextra;
 
-                                       g_hash_table_iter_init (&it, mres->symbols);
                                        i = 0;
 
-                                       while (g_hash_table_iter_next (&it, &k, &v)) {
+                                       kh_foreach_value_ptr (mres->symbols, sym, {
                                                id = rspamd_symbols_cache_find_symbol (task->cfg->cache,
-                                                               k);
-                                               sym = v;
+                                                               sym->name);
 
                                                if (id >= 0) {
                                                        ls->results[i].id = id;
@@ -1592,7 +1583,7 @@ rspamd_protocol_write_log_pipe (struct rspamd_task *task)
                                                }
 
                                                i ++;
-                                       }
+                                       });
 
                                        memcpy (&ls->results[n], extra->data, nextra * sizeof (er));
                                }
index 6dbd4d338aa7fd2f30c4049ef81fcebf35552327..3df59781663a81b02ce14878e2c9c77e9d61d44f 100644 (file)
@@ -159,9 +159,9 @@ rspamd_roll_history_update (struct roll_history *history,
                row->required_score = rspamd_task_get_required_score (task, metric_res);
                cbdata.pos = row->symbols;
                cbdata.remain = sizeof (row->symbols);
-               g_hash_table_foreach (metric_res->symbols,
-                       roll_history_symbols_callback,
-                       &cbdata);
+               rspamd_task_symbol_result_foreach (task,
+                               roll_history_symbols_callback,
+                               &cbdata);
                if (cbdata.remain > 0) {
                        /* Remove last whitespace and comma */
                        *cbdata.pos-- = '\0';
index d881c68974b9a1138680297be9a1507aa5380ab2..437bc48294c7f2e91368f73a01fa0e2c938ce915 100644 (file)
@@ -1045,9 +1045,7 @@ rspamd_task_log_metric_res (struct rspamd_task *task,
        static gchar scorebuf[32];
        rspamd_ftok_t res = {.begin = NULL, .len = 0};
        struct rspamd_metric_result *mres;
-       GHashTableIter it;
        gboolean first = TRUE;
-       gpointer k, v;
        rspamd_fstring_t *symbuf;
        struct rspamd_symbol_result *sym;
        GPtrArray *sorted_symbols;
@@ -1083,16 +1081,13 @@ rspamd_task_log_metric_res (struct rspamd_task *task,
                        break;
                case RSPAMD_LOG_SYMBOLS:
                        symbuf = rspamd_fstring_sized_new (128);
-                       g_hash_table_iter_init (&it, mres->symbols);
-                       sorted_symbols = g_ptr_array_sized_new (g_hash_table_size (mres->symbols));
-
-                       while (g_hash_table_iter_next (&it, &k, &v)) {
-                               sym = v;
+                       sorted_symbols = g_ptr_array_sized_new (kh_size (mres->symbols));
 
+                       kh_foreach_value_ptr (mres->symbols, sym, {
                                if (!(sym->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) {
-                                       g_ptr_array_add (sorted_symbols, v);
+                                       g_ptr_array_add (sorted_symbols, (gpointer)sym);
                                }
-                       }
+                       });
 
                        g_ptr_array_sort (sorted_symbols, rspamd_task_compare_log_sym);
 
index 978c8f3c0506181503a6006169ffebdb2998fd24..f58bf6150d556fff5fd96d4cb43d8249c6e5f676 100644 (file)
@@ -1093,7 +1093,7 @@ rspamd_stat_has_classifier_symbols (struct rspamd_task *task,
                id = g_array_index (cl->statfiles_ids, gint, i);
                st = g_ptr_array_index (st_ctx->statfiles, id);
 
-               if (g_hash_table_lookup (mres->symbols, st->stcf->symbol)) {
+               if (rspamd_task_find_symbol_result (task, st->stcf->symbol)) {
                        if (is_spam == !!st->stcf->is_spam) {
                                msg_debug_task ("do not autolearn %s as symbol %s is already "
                                                "added", is_spam ? "spam" : "ham", st->stcf->symbol);
index 94239e5acd0dcf6031cabf9776c5a50256c93925..ca10a94e7d9c002ea9f74bc9f59a363b2ed6cf9d 100644 (file)
@@ -1446,7 +1446,7 @@ lua_task_adjust_result (lua_State * L)
                metric_res = task->result;
 
                if (metric_res) {
-                       s = g_hash_table_lookup (metric_res->symbols, symbol_name);
+                       s = rspamd_task_find_symbol_result (task, symbol_name);
                }
                else {
                        return luaL_error (L, "no metric result");
@@ -3246,7 +3246,7 @@ lua_push_symbol_result (lua_State *L,
                metric_res = task->result;
 
                if (metric_res) {
-                       s = g_hash_table_lookup (metric_res->symbols, symbol);
+                       s = rspamd_task_find_symbol_result (task, symbol);
                }
        }
        else {
@@ -3295,7 +3295,7 @@ lua_push_symbol_result (lua_State *L,
 
                if (s->options) {
                        lua_pushstring (L, "options");
-                       lua_createtable (L, g_hash_table_size (s->options), 0);
+                       lua_createtable (L, kh_size (s->options), 0);
 
                        DL_FOREACH (s->opts_head, opt) {
                                lua_pushstring (L, (const char*)opt->option);
@@ -3349,18 +3349,12 @@ lua_task_has_symbol (lua_State *L)
 {
        struct rspamd_task *task = lua_check_task (L, 1);
        const gchar *symbol;
-       struct rspamd_metric_result *mres;
        gboolean found = FALSE;
 
        symbol = luaL_checkstring (L, 2);
 
        if (task && symbol) {
-               mres = task->result;
-
-               if (mres) {
-                       found = g_hash_table_lookup (mres->symbols, symbol) != NULL;
-               }
-
+               found = (rspamd_task_find_symbol_result (task, symbol) != NULL);
                lua_pushboolean (L, found);
        }
        else {
@@ -3376,28 +3370,24 @@ lua_task_get_symbols (lua_State *L)
        struct rspamd_task *task = lua_check_task (L, 1);
        struct rspamd_metric_result *mres;
        gint i = 1;
-       GHashTableIter it;
-       gpointer k, v;
        struct rspamd_symbol_result *s;
 
        if (task) {
                mres = task->result;
 
                if (mres) {
-                       lua_createtable (L, g_hash_table_size (mres->symbols), 0);
-                       lua_createtable (L, g_hash_table_size (mres->symbols), 0);
-                       g_hash_table_iter_init (&it, mres->symbols);
+                       lua_createtable (L, kh_size (mres->symbols), 0);
+                       lua_createtable (L, kh_size (mres->symbols), 0);
 
-                       while (g_hash_table_iter_next (&it, &k, &v)) {
-                               s = v;
+                       kh_foreach_value_ptr (mres->symbols, s, {
                                if (!(s->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) {
-                                       lua_pushstring (L, k);
+                                       lua_pushstring (L, s->name);
                                        lua_rawseti (L, -3, i);
                                        lua_pushnumber (L, s->score);
                                        lua_rawseti (L, -2, i);
                                        i++;
                                }
-                       }
+                       });
                }
                else {
                        lua_createtable (L, 0, 0);
@@ -3416,8 +3406,7 @@ lua_task_get_symbols_all (lua_State *L)
 {
        struct rspamd_task *task = lua_check_task (L, 1);
        struct rspamd_metric_result *mres;
-       GHashTableIter it;
-       gpointer k, v;
+       struct rspamd_symbol_result *s;
        gboolean found = FALSE;
        gint i = 1;
 
@@ -3426,13 +3415,12 @@ lua_task_get_symbols_all (lua_State *L)
 
                if (mres) {
                        found = TRUE;
-                       lua_createtable (L, g_hash_table_size (mres->symbols), 0);
-                       g_hash_table_iter_init (&it, mres->symbols);
+                       lua_createtable (L, kh_size (mres->symbols), 0);
 
-                       while (g_hash_table_iter_next (&it, &k, &v)) {
-                               lua_push_symbol_result (L, task, k, v, FALSE, TRUE);
+                       kh_foreach_value_ptr (mres->symbols, s, {
+                               lua_push_symbol_result (L, task, s->name, s, FALSE, TRUE);
                                lua_rawseti (L, -2, i++);
-                       }
+                       });
                }
        }
        else {
@@ -3453,32 +3441,28 @@ lua_task_get_symbols_numeric (lua_State *L)
        struct rspamd_task *task = lua_check_task (L, 1);
        struct rspamd_metric_result *mres;
        gint i = 1, id;
-       GHashTableIter it;
-       gpointer k, v;
        struct rspamd_symbol_result *s;
 
        if (task) {
                mres = task->result;
 
                if (mres) {
-                       lua_createtable (L, g_hash_table_size (mres->symbols), 0);
-                       lua_createtable (L, g_hash_table_size (mres->symbols), 0);
-
-                       g_hash_table_iter_init (&it, mres->symbols);
+                       lua_createtable (L, kh_size (mres->symbols), 0);
+                       lua_createtable (L, kh_size (mres->symbols), 0);
 
-                       while (g_hash_table_iter_next (&it, &k, &v)) {
-                               s = v;
+                       lua_createtable (L, kh_size (mres->symbols), 0);
 
+                       kh_foreach_value_ptr (mres->symbols, s, {
                                if (!(s->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) {
                                        id = rspamd_symbols_cache_find_symbol (task->cfg->cache,
-                                                       k);
+                                                       s->name);
                                        lua_pushnumber (L, id);
                                        lua_rawseti (L, -3, i);
                                        lua_pushnumber (L, s->score);
                                        lua_rawseti (L, -2, i);
                                        i++;
                                }
-                       }
+                       });
                }
                else {
                        lua_createtable (L, 0, 0);
@@ -3502,7 +3486,6 @@ struct tokens_foreach_cbdata {
 static void
 tokens_foreach_cb (gint id, const gchar *sym, gint flags, gpointer ud)
 {
-       struct rspamd_metric_result *mres;
        struct tokens_foreach_cbdata *cbd = ud;
        struct rspamd_symbol_result *s;
 
@@ -3510,9 +3493,7 @@ tokens_foreach_cb (gint id, const gchar *sym, gint flags, gpointer ud)
                return;
        }
 
-       mres = cbd->task->result;
-
-       if (mres && (s = g_hash_table_lookup (mres->symbols, sym)) != NULL) {
+       if ((s = rspamd_task_find_symbol_result (cbd->task, sym)) != NULL) {
                if (cbd->normalize) {
                        lua_pushnumber (cbd->L, tanh (s->score));
                }