]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Rework] Use more c++ in cfg_utils
authorVsevolod Stakhov <vsevolod@rspamd.com>
Mon, 14 Aug 2023 11:15:55 +0000 (12:15 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Mon, 14 Aug 2023 11:15:55 +0000 (12:15 +0100)
src/libserver/CMakeLists.txt
src/libserver/cfg_file.h
src/libserver/cfg_file_private.h
src/libserver/cfg_utils.cxx [moved from src/libserver/cfg_utils.c with 81% similarity]
src/libutil/mem_pool.c
src/libutil/mem_pool.h
src/lua/lua_common.h

index c4940f9171cb83da6eb75011ec64e200ad8a1ac8..701ed42d0b6b78ce91900731d6079fbd22492af8 100644 (file)
@@ -1,49 +1,49 @@
 # Librspamdserver
 ADD_SUBDIRECTORY(css)
 SET(LIBRSPAMDSERVERSRC
-                               ${CMAKE_CURRENT_SOURCE_DIR}/cfg_utils.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/cfg_rcl.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/composites/composites.cxx
-                               ${CMAKE_CURRENT_SOURCE_DIR}/composites/composites_manager.cxx
-                               ${CMAKE_CURRENT_SOURCE_DIR}/dkim.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/dns.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/dynamic_cfg.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/async_session.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/fuzzy_backend/fuzzy_backend.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/fuzzy_backend/fuzzy_backend_sqlite.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/fuzzy_backend/fuzzy_backend_redis.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/milter.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/monitored.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/protocol.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/re_cache.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/redis_pool.cxx
-                               ${CMAKE_CURRENT_SOURCE_DIR}/roll_history.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/spf.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/ssl_util.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/symcache/symcache_impl.cxx
-                               ${CMAKE_CURRENT_SOURCE_DIR}/symcache/symcache_item.cxx
-                               ${CMAKE_CURRENT_SOURCE_DIR}/symcache/symcache_runtime.cxx
-                               ${CMAKE_CURRENT_SOURCE_DIR}/symcache/symcache_c.cxx
-                               ${CMAKE_CURRENT_SOURCE_DIR}/task.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/url.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/worker_util.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/logger/logger.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/logger/logger_file.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/logger/logger_syslog.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/logger/logger_console.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/http/http_util.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/http/http_message.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/http/http_connection.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/http/http_router.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/http/http_context.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/maps/map.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/maps/map_helpers.c
-                               ${CMAKE_CURRENT_SOURCE_DIR}/html/html_entities.cxx
-                               ${CMAKE_CURRENT_SOURCE_DIR}/html/html_url.cxx
-                               ${CMAKE_CURRENT_SOURCE_DIR}/html/html.cxx
-                               ${CMAKE_CURRENT_SOURCE_DIR}/html/html_tests.cxx
-                               ${CMAKE_CURRENT_SOURCE_DIR}/hyperscan_tools.cxx
-                               ${LIBCSSSRC})
+        ${CMAKE_CURRENT_SOURCE_DIR}/cfg_utils.cxx
+        ${CMAKE_CURRENT_SOURCE_DIR}/cfg_rcl.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/composites/composites.cxx
+        ${CMAKE_CURRENT_SOURCE_DIR}/composites/composites_manager.cxx
+        ${CMAKE_CURRENT_SOURCE_DIR}/dkim.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/dns.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/dynamic_cfg.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/async_session.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/fuzzy_backend/fuzzy_backend.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/fuzzy_backend/fuzzy_backend_sqlite.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/fuzzy_backend/fuzzy_backend_redis.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/milter.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/monitored.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/protocol.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/re_cache.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/redis_pool.cxx
+        ${CMAKE_CURRENT_SOURCE_DIR}/roll_history.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/spf.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/ssl_util.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/symcache/symcache_impl.cxx
+        ${CMAKE_CURRENT_SOURCE_DIR}/symcache/symcache_item.cxx
+        ${CMAKE_CURRENT_SOURCE_DIR}/symcache/symcache_runtime.cxx
+        ${CMAKE_CURRENT_SOURCE_DIR}/symcache/symcache_c.cxx
+        ${CMAKE_CURRENT_SOURCE_DIR}/task.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/url.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/worker_util.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/logger/logger.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/logger/logger_file.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/logger/logger_syslog.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/logger/logger_console.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/http/http_util.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/http/http_message.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/http/http_connection.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/http/http_router.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/http/http_context.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/maps/map.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/maps/map_helpers.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/html/html_entities.cxx
+        ${CMAKE_CURRENT_SOURCE_DIR}/html/html_url.cxx
+        ${CMAKE_CURRENT_SOURCE_DIR}/html/html.cxx
+        ${CMAKE_CURRENT_SOURCE_DIR}/html/html_tests.cxx
+        ${CMAKE_CURRENT_SOURCE_DIR}/hyperscan_tools.cxx
+        ${LIBCSSSRC})
 
 # Librspamd-server
 SET(RSPAMD_SERVER ${LIBRSPAMDSERVERSRC} PARENT_SCOPE)
index 9b09608f2276bd7677d6facc439a024077609579..b4b60a92f2c72072384aca26260f3e595efc685a 100644 (file)
@@ -1,11 +1,11 @@
-/*-
- * Copyright 2016-2017 Vsevolod Stakhov
+/*
+ * Copyright 2023 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -487,7 +487,7 @@ struct rspamd_config {
 
        struct rspamd_external_libs_ctx *libs_ctx;  /**< context for external libraries                                         */
        struct rspamd_monitored_ctx *monitored_ctx; /**< context for monitored resources                                        */
-       struct rspamd_redis_pool *redis_pool;       /**< redis connection pool                                                          */
+       void *redis_pool;                           /**< redis connection pool                                                          */
 
        struct rspamd_re_cache *re_cache; /**< static regexp cache                                                              */
 
index d221e8427ac7018b5c37017e9551f3b8eb973539..1a1500d09bacaf21c5dcb6e8f3b24b3164a8ba10 100644 (file)
@@ -1,11 +1,11 @@
-/*-
- * Copyright 2019 Vsevolod Stakhov
+/*
+ * Copyright 2023 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -31,7 +31,7 @@ extern "C" {
  */
 struct rspamd_action {
        enum rspamd_action_type action_type;
-       enum rspamd_action_flags flags;
+       int flags; /* enum rspamd_action_flags */
        guint priority;
        gint lua_handler_ref; /* If special handling is needed */
        gdouble threshold;
similarity index 81%
rename from src/libserver/cfg_utils.c
rename to src/libserver/cfg_utils.cxx
index cdb1518a854583be6b53520e28acc52b1775b713..d3999808015edcffce7bca3c5775834061c61fe2 100644 (file)
 
 #include "blas-config.h"
 
+#include <string>
+#include <string_view>
+#include "fmt/core.h"
+#include "cxx/util.hxx"
+#include "frozen/unordered_map.h"
+#include "frozen/string.h"
+
 #define DEFAULT_SCORE 10.0
 
 #define DEFAULT_RLIMIT_NOFILE 2048
 
 struct rspamd_ucl_map_cbdata {
        struct rspamd_config *cfg;
-       GString *buf;
+       std::string buf;
+
+       explicit rspamd_ucl_map_cbdata(struct rspamd_config *cfg)
+               : cfg(cfg)
+       {
+       }
 };
 static gchar *rspamd_ucl_read_cb(gchar *chunk,
                                                                 gint len,
@@ -108,25 +120,29 @@ rspamd_parse_bind_line(struct rspamd_config *cfg,
        const gchar *fdname;
        gboolean ret = TRUE;
 
-       if (str == NULL) {
+       if (str == nullptr) {
                return FALSE;
        }
 
-       cnf = g_malloc0(sizeof(struct rspamd_worker_bind_conf));
+       cnf = rspamd_mempool_alloc0_type(cfg->cfg_pool, struct rspamd_worker_bind_conf);
 
        cnf->cnt = 1024;
-       cnf->bind_line = g_strdup(str);
+       cnf->bind_line = rspamd_mempool_strdup(cfg->cfg_pool, str);
+
+       auto bind_line = std::string_view{cnf->bind_line};
 
-       if (g_ascii_strncasecmp(str, "systemd:", sizeof("systemd:") - 1) == 0) {
+       if (bind_line.starts_with("systemd:")) {
                /* The actual socket will be passed by systemd environment */
                fdname = str + sizeof("systemd:") - 1;
                cnf->is_systemd = TRUE;
-               cnf->addrs = g_ptr_array_new_full(1, g_free);
+               cnf->addrs = g_ptr_array_new_full(1, nullptr);
+               rspamd_mempool_add_destructor(cfg->cfg_pool,
+                                                                         rspamd_ptr_array_free_hard, cnf->addrs);
 
                if (fdname[0]) {
-                       g_ptr_array_add(cnf->addrs, g_strdup(fdname));
+                       g_ptr_array_add(cnf->addrs, rspamd_mempool_strdup(cfg->cfg_pool, fdname));
                        cnf->cnt = cnf->addrs->len;
-                       cnf->name = g_strdup(str);
+                       cnf->name = rspamd_mempool_strdup(cfg->cfg_pool, str);
                        LL_PREPEND(cf->bind_conf, cnf);
                }
                else {
@@ -136,7 +152,7 @@ rspamd_parse_bind_line(struct rspamd_config *cfg,
        }
        else {
                if (rspamd_parse_host_port_priority(str, &cnf->addrs,
-                                                                                       NULL, &cnf->name, DEFAULT_BIND_PORT, TRUE, NULL) == RSPAMD_PARSE_ADDR_FAIL) {
+                                                                                       nullptr, &cnf->name, DEFAULT_BIND_PORT, TRUE, cfg->cfg_pool) == RSPAMD_PARSE_ADDR_FAIL) {
                        msg_err_config("cannot parse bind line: %s", str);
                        ret = FALSE;
                }
@@ -146,15 +162,6 @@ rspamd_parse_bind_line(struct rspamd_config *cfg,
                }
        }
 
-       if (!ret) {
-               if (cnf->addrs) {
-                       g_ptr_array_free(cnf->addrs, TRUE);
-               }
-
-               g_free(cnf->name);
-               g_free(cnf);
-       }
-
        return ret;
 }
 
@@ -178,11 +185,11 @@ rspamd_config_new(enum rspamd_config_init_flags flags)
        for (int i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i++) {
                struct rspamd_action *action;
 
-               action = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*action));
+               action = rspamd_mempool_alloc0_type(cfg->cfg_pool, struct rspamd_action);
                action->threshold = NAN;
                action->name = rspamd_mempool_strdup(cfg->cfg_pool,
-                                                                                        rspamd_action_to_str(i));
-               action->action_type = i;
+                                                                                        rspamd_action_to_str(static_cast<rspamd_action_type>(i)));
+               action->action_type = static_cast<rspamd_action_type>(i);
 
                if (i == METRIC_ACTION_SOFT_REJECT) {
                        action->flags |= RSPAMD_ACTION_NO_THRESHOLD | RSPAMD_ACTION_HAM;
@@ -233,11 +240,12 @@ rspamd_config_new(enum rspamd_config_init_flags flags)
        cfg->max_opts_len = 4096;
 
        /* Default log line */
-       cfg->log_format_str = "id: <$mid>,$if_qid{ qid: <$>,}$if_ip{ ip: $,}"
-                                                 "$if_user{ user: $,}$if_smtp_from{ from: <$>,} (default: $is_spam "
-                                                 "($action): [$scores] [$symbols_scores_params]), len: $len, time: $time_real, "
-                                                 "dns req: $dns_req, digest: <$digest>"
-                                                 "$if_smtp_rcpts{ rcpts: <$>, }$if_mime_rcpt{ mime_rcpt: <$>, }";
+       cfg->log_format_str = rspamd_mempool_strdup(cfg->cfg_pool,
+                                                                                               "id: <$mid>,$if_qid{ qid: <$>,}$if_ip{ ip: $,}"
+                                                                                               "$if_user{ user: $,}$if_smtp_from{ from: <$>,} (default: $is_spam "
+                                                                                               "($action): [$scores] [$symbols_scores_params]), len: $len, time: $time_real, "
+                                                                                               "dns req: $dns_req, digest: <$digest>"
+                                                                                               "$if_smtp_rcpts{ rcpts: <$>, }$if_mime_rcpt{ mime_rcpt: <$>, }");
        /* Allow non-mime input by default */
        cfg->allow_raw_input = TRUE;
        /* Default maximum words processed */
@@ -252,12 +260,12 @@ rspamd_config_new(enum rspamd_config_init_flags flags)
        cfg->full_gc_iters = DEFAULT_GC_MAXITERS;
 
        /* Default hyperscan cache */
-       cfg->hs_cache_dir = RSPAMD_DBDIR "/";
+       cfg->hs_cache_dir = rspamd_mempool_strdup(cfg->cfg_pool, RSPAMD_DBDIR "/");
 
        if (!(flags & RSPAMD_CONFIG_INIT_SKIP_LUA)) {
-               cfg->lua_state = rspamd_lua_init(flags & RSPAMD_CONFIG_INIT_WIPE_LUA_MEM);
+               cfg->lua_state = (void *) rspamd_lua_init(flags & RSPAMD_CONFIG_INIT_WIPE_LUA_MEM);
                cfg->own_lua_state = TRUE;
-               cfg->lua_thread_pool = lua_thread_pool_new(cfg->lua_state);
+               cfg->lua_thread_pool = (void *) lua_thread_pool_new(RSPAMD_LUA_CFG_STATE(cfg));
        }
 
        cfg->cache = rspamd_symcache_new(cfg);
@@ -269,7 +277,7 @@ rspamd_config_new(enum rspamd_config_init_flags flags)
         */
        cfg->enable_shutdown_workaround = TRUE;
 
-       cfg->ssl_ciphers = "HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4";
+       cfg->ssl_ciphers = rspamd_mempool_strdup(cfg->cfg_pool, "HIGH:!anullptr:!kRSA:!PSK:!SRP:!MD5:!RC4");
        cfg->max_message = DEFAULT_MAX_MESSAGE;
        cfg->max_pic_size = DEFAULT_MAX_PIC;
        cfg->images_cache_size = 256;
@@ -295,27 +303,27 @@ void rspamd_config_free(struct rspamd_config *cfg)
        struct rspamd_config_settings_elt *set, *stmp;
        struct rspamd_worker_log_pipe *lp, *ltmp;
 
-       rspamd_lua_run_config_unload(cfg->lua_state, cfg);
+       rspamd_lua_run_config_unload(RSPAMD_LUA_CFG_STATE(cfg), cfg);
 
        /* Scripts part */
        DL_FOREACH_SAFE(cfg->on_term_scripts, sc, sctmp)
        {
-               luaL_unref(cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref);
+               luaL_unref(RSPAMD_LUA_CFG_STATE(cfg), LUA_REGISTRYINDEX, sc->cbref);
        }
 
        DL_FOREACH_SAFE(cfg->on_load_scripts, sc, sctmp)
        {
-               luaL_unref(cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref);
+               luaL_unref(RSPAMD_LUA_CFG_STATE(cfg), LUA_REGISTRYINDEX, sc->cbref);
        }
 
        DL_FOREACH_SAFE(cfg->post_init_scripts, sc, sctmp)
        {
-               luaL_unref(cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref);
+               luaL_unref(RSPAMD_LUA_CFG_STATE(cfg), LUA_REGISTRYINDEX, sc->cbref);
        }
 
        DL_FOREACH_SAFE(cfg->config_unload_scripts, sc, sctmp)
        {
-               luaL_unref(cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref);
+               luaL_unref(RSPAMD_LUA_CFG_STATE(cfg), LUA_REGISTRYINDEX, sc->cbref);
        }
 
        DL_FOREACH_SAFE(cfg->setting_ids, set, stmp)
@@ -348,9 +356,9 @@ void rspamd_config_free(struct rspamd_config *cfg)
                rspamd_monitored_ctx_destroy(cfg->monitored_ctx);
        }
 
-       if (cfg->lua_state && cfg->own_lua_state) {
-               lua_thread_pool_free(cfg->lua_thread_pool);
-               rspamd_lua_close(cfg->lua_state);
+       if (RSPAMD_LUA_CFG_STATE(cfg) && cfg->own_lua_state) {
+               lua_thread_pool_free((struct lua_thread_pool *) cfg->lua_thread_pool);
+               rspamd_lua_close(RSPAMD_LUA_CFG_STATE(cfg));
        }
 
        if (cfg->redis_pool) {
@@ -382,10 +390,10 @@ rspamd_config_get_module_opt(struct rspamd_config *cfg,
                                                         const gchar *module_name,
                                                         const gchar *opt_name)
 {
-       const ucl_object_t *res = NULL, *sec;
+       const ucl_object_t *res = nullptr, *sec;
 
        sec = ucl_obj_get_key(cfg->rcl_obj, module_name);
-       if (sec != NULL) {
+       if (sec != nullptr) {
                res = ucl_obj_get_key(sec, opt_name);
        }
 
@@ -445,173 +453,122 @@ gint rspamd_config_parse_flag(const gchar *str, guint len)
        return -1;
 }
 
+// A mapping between names and log format types + flags
+constexpr const auto config_vars = frozen::make_unordered_map<frozen::string, std::pair<rspamd_log_format_type, int>>({
+       {"mid", {RSPAMD_LOG_MID, 0}},
+       {"qid", {RSPAMD_LOG_QID, 0}},
+       {"user", {RSPAMD_LOG_USER, 0}},
+       {"ip", {RSPAMD_LOG_IP, 0}},
+       {"len", {RSPAMD_LOG_LEN, 0}},
+       {"dns_req", {RSPAMD_LOG_DNS_REQ, 0}},
+       {"smtp_from", {RSPAMD_LOG_SMTP_FROM, 0}},
+       {"mime_from", {RSPAMD_LOG_MIME_FROM, 0}},
+       {"smtp_rcpt", {RSPAMD_LOG_SMTP_RCPT, 0}},
+       {"mime_rcpt", {RSPAMD_LOG_MIME_RCPT, 0}},
+       {"smtp_rcpts", {RSPAMD_LOG_SMTP_RCPTS, 0}},
+       {"mime_rcpts", {RSPAMD_LOG_MIME_RCPTS, 0}},
+       {"time_real", {RSPAMD_LOG_TIME_REAL, 0}},
+       {"time_virtual", {RSPAMD_LOG_TIME_VIRTUAL, 0}},
+       {"lua", {RSPAMD_LOG_LUA, 0}},
+       {"digest", {RSPAMD_LOG_DIGEST, 0}},
+       {"checksum", {RSPAMD_LOG_DIGEST, 0}},
+       {"filename", {RSPAMD_LOG_FILENAME, 0}},
+       {"forced_action", {RSPAMD_LOG_FORCED_ACTION, 0}},
+       {"settings_id", {RSPAMD_LOG_SETTINGS_ID, 0}},
+       {"mempool_size", {RSPAMD_LOG_MEMPOOL_SIZE, 0}},
+       {"mempool_waste", {RSPAMD_LOG_MEMPOOL_WASTE, 0}},
+       {"action", {RSPAMD_LOG_ACTION, 0}},
+       {"scores", {RSPAMD_LOG_SCORES, 0}},
+       {"symbols", {RSPAMD_LOG_SYMBOLS, 0}},
+       {"symbols_scores", {RSPAMD_LOG_SYMBOLS, RSPAMD_LOG_FMT_FLAG_SYMBOLS_SCORES}},
+       {"symbols_params", {RSPAMD_LOG_SYMBOLS, RSPAMD_LOG_FMT_FLAG_SYMBOLS_PARAMS}},
+       {"symbols_scores_params", {RSPAMD_LOG_SYMBOLS, RSPAMD_LOG_FMT_FLAG_SYMBOLS_PARAMS | RSPAMD_LOG_FMT_FLAG_SYMBOLS_SCORES}},
+       {"groups", {RSPAMD_LOG_GROUPS, 0}},
+       {"public_groups", {RSPAMD_LOG_PUBLIC_GROUPS, 0}},
+});
+
 static gboolean
 rspamd_config_process_var(struct rspamd_config *cfg, const rspamd_ftok_t *var,
                                                  const rspamd_ftok_t *content)
 {
-       guint flags = RSPAMD_LOG_FLAG_DEFAULT;
-       struct rspamd_log_format *lf;
-       enum rspamd_log_format_type type;
-       rspamd_ftok_t tok;
-       gint id;
+       g_assert(var != nullptr);
 
-       g_assert(var != NULL);
+       auto flags = 0;
+       auto lc_var = std::string{var->begin, var->len};
+       std::transform(lc_var.begin(), lc_var.end(), lc_var.begin(), g_ascii_tolower);
+       auto tok = std::string_view{lc_var};
 
-       if (var->len > 3 && rspamd_lc_cmp(var->begin, "if_", 3) == 0) {
+       if (var->len > 3 && tok.starts_with("if_")) {
                flags |= RSPAMD_LOG_FMT_FLAG_CONDITION;
-               tok.begin = var->begin + 3;
-               tok.len = var->len - 3;
-       }
-       else {
-               tok.begin = var->begin;
-               tok.len = var->len;
+               tok = tok.substr(3);
        }
 
-       /* Now compare variable and check what we have */
-       if (rspamd_ftok_cstr_equal(&tok, "mid", TRUE)) {
-               type = RSPAMD_LOG_MID;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "qid", TRUE)) {
-               type = RSPAMD_LOG_QID;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "user", TRUE)) {
-               type = RSPAMD_LOG_USER;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "is_spam", TRUE)) {
-               type = RSPAMD_LOG_ISSPAM;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "action", TRUE)) {
-               type = RSPAMD_LOG_ACTION;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "scores", TRUE)) {
-               type = RSPAMD_LOG_SCORES;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "symbols", TRUE)) {
-               type = RSPAMD_LOG_SYMBOLS;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "symbols_scores", TRUE)) {
-               type = RSPAMD_LOG_SYMBOLS;
-               flags |= RSPAMD_LOG_FMT_FLAG_SYMBOLS_SCORES;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "symbols_params", TRUE)) {
-               type = RSPAMD_LOG_SYMBOLS;
-               flags |= RSPAMD_LOG_FMT_FLAG_SYMBOLS_PARAMS;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "symbols_scores_params", TRUE)) {
-               type = RSPAMD_LOG_SYMBOLS;
-               flags |= RSPAMD_LOG_FMT_FLAG_SYMBOLS_PARAMS | RSPAMD_LOG_FMT_FLAG_SYMBOLS_SCORES;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "groups", TRUE)) {
-               type = RSPAMD_LOG_GROUPS;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "public_groups", TRUE)) {
-               type = RSPAMD_LOG_PUBLIC_GROUPS;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "ip", TRUE)) {
-               type = RSPAMD_LOG_IP;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "len", TRUE)) {
-               type = RSPAMD_LOG_LEN;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "dns_req", TRUE)) {
-               type = RSPAMD_LOG_DNS_REQ;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "smtp_from", TRUE)) {
-               type = RSPAMD_LOG_SMTP_FROM;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "mime_from", TRUE)) {
-               type = RSPAMD_LOG_MIME_FROM;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "smtp_rcpt", TRUE)) {
-               type = RSPAMD_LOG_SMTP_RCPT;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "mime_rcpt", TRUE)) {
-               type = RSPAMD_LOG_MIME_RCPT;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "smtp_rcpts", TRUE)) {
-               type = RSPAMD_LOG_SMTP_RCPTS;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "mime_rcpts", TRUE)) {
-               type = RSPAMD_LOG_MIME_RCPTS;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "time_real", TRUE)) {
-               type = RSPAMD_LOG_TIME_REAL;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "time_virtual", TRUE)) {
-               type = RSPAMD_LOG_TIME_VIRTUAL;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "lua", TRUE)) {
-               type = RSPAMD_LOG_LUA;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "digest", TRUE) ||
-                        rspamd_ftok_cstr_equal(&tok, "checksum", TRUE)) {
-               type = RSPAMD_LOG_DIGEST;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "filename", TRUE)) {
-               type = RSPAMD_LOG_FILENAME;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "forced_action", TRUE)) {
-               type = RSPAMD_LOG_FORCED_ACTION;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "settings_id", TRUE)) {
-               type = RSPAMD_LOG_SETTINGS_ID;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "mempool_size", TRUE)) {
-               type = RSPAMD_LOG_MEMPOOL_SIZE;
-       }
-       else if (rspamd_ftok_cstr_equal(&tok, "mempool_waste", TRUE)) {
-               type = RSPAMD_LOG_MEMPOOL_WASTE;
-       }
-       else {
-               msg_err_config("unknown log variable: %T", &tok);
-               return FALSE;
-       }
+       auto maybe_fmt_var = rspamd::find_map(config_vars, tok);
 
-       lf = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*lf));
-       lf->type = type;
-       lf->flags = flags;
+       if (maybe_fmt_var) {
+               auto &fmt_var = maybe_fmt_var.value().get();
+               auto *log_format = rspamd_mempool_alloc0_type(cfg->cfg_pool, rspamd_log_format);
 
-       if (type != RSPAMD_LOG_LUA) {
-               if (content && content->len > 0) {
-                       lf->data = rspamd_mempool_alloc0(cfg->cfg_pool,
-                                                                                        sizeof(rspamd_ftok_t));
-                       memcpy(lf->data, content, sizeof(*content));
-                       lf->len = sizeof(*content);
-               }
-       }
-       else {
-               /* Load lua code and ensure that we have function ref returned */
-               if (!content || content->len == 0) {
-                       msg_err_config("lua variable needs content: %T", &tok);
-                       return FALSE;
-               }
+               log_format->type = fmt_var.first;
+               log_format->flags = fmt_var.second | flags;
 
-               if (luaL_loadbuffer(cfg->lua_state, content->begin, content->len,
-                                                       "lua log variable") != 0) {
-                       msg_err_config("error loading lua code: '%T': %s", content,
-                                                  lua_tostring(cfg->lua_state, -1));
-                       return FALSE;
+               if (log_format->type != RSPAMD_LOG_LUA) {
+                       if (content && content->len > 0) {
+                               log_format->data = rspamd_mempool_alloc0(cfg->cfg_pool,
+                                                                                                                sizeof(rspamd_ftok_t));
+                               memcpy(log_format->data, content, sizeof(*content));
+                               log_format->len = sizeof(*content);
+                       }
                }
-               if (lua_pcall(cfg->lua_state, 0, 1, 0) != 0) {
-                       msg_err_config("error executing lua code: '%T': %s", content,
-                                                  lua_tostring(cfg->lua_state, -1));
-                       lua_pop(cfg->lua_state, 1);
+               else {
+                       /* Load lua code and ensure that we have function ref returned */
+                       if (!content || content->len == 0) {
+                               msg_err_config("lua variable needs content: %T", &tok);
+                               return FALSE;
+                       }
 
-                       return FALSE;
-               }
+                       if (luaL_loadbuffer(RSPAMD_LUA_CFG_STATE(cfg), content->begin, content->len,
+                                                               "lua log variable") != 0) {
+                               msg_err_config("error loading lua code: '%T': %s", content,
+                                                          lua_tostring(RSPAMD_LUA_CFG_STATE(cfg), -1));
+                               return FALSE;
+                       }
+                       if (lua_pcall(RSPAMD_LUA_CFG_STATE(cfg), 0, 1, 0) != 0) {
+                               msg_err_config("error executing lua code: '%T': %s", content,
+                                                          lua_tostring(RSPAMD_LUA_CFG_STATE(cfg), -1));
+                               lua_pop(RSPAMD_LUA_CFG_STATE(cfg), 1);
 
-               if (lua_type(cfg->lua_state, -1) != LUA_TFUNCTION) {
-                       msg_err_config("lua variable should return function: %T", content);
-                       lua_pop(cfg->lua_state, 1);
-                       return FALSE;
+                               return FALSE;
+                       }
+
+                       if (lua_type(RSPAMD_LUA_CFG_STATE(cfg), -1) != LUA_TFUNCTION) {
+                               msg_err_config("lua variable should return function: %T", content);
+                               lua_pop(RSPAMD_LUA_CFG_STATE(cfg), 1);
+                               return FALSE;
+                       }
+
+                       auto id = luaL_ref(RSPAMD_LUA_CFG_STATE(cfg), LUA_REGISTRYINDEX);
+                       log_format->data = GINT_TO_POINTER(id);
+                       log_format->len = 0;
                }
 
-               id = luaL_ref(cfg->lua_state, LUA_REGISTRYINDEX);
-               lf->data = GINT_TO_POINTER(id);
-               lf->len = 0;
+               DL_APPEND(cfg->log_format, log_format);
        }
+       else {
+               std::string known_formats;
 
-       DL_APPEND(cfg->log_format, lf);
+               for (const auto &v: config_vars) {
+                       known_formats += std::string_view{v.first.data(), v.first.size()};
+                       known_formats += ", ";
+               }
+
+               if (known_formats.size() > 2) {
+                       // Remove last comma
+                       known_formats.resize(known_formats.size() - 2);
+               }
+               msg_err_config("unknown log variable: %T, known vars are: \"%s\"", var, known_formats.c_str());
+               return FALSE;
+       }
 
        return TRUE;
 }
@@ -621,7 +578,7 @@ rspamd_config_parse_log_format(struct rspamd_config *cfg)
 {
        const gchar *p, *c, *end, *s;
        gchar *d;
-       struct rspamd_log_format *lf = NULL;
+       struct rspamd_log_format *lf = nullptr;
        rspamd_ftok_t var, var_content;
        enum {
                parse_str,
@@ -631,10 +588,10 @@ rspamd_config_parse_log_format(struct rspamd_config *cfg)
        } state = parse_str;
        gint braces = 0;
 
-       g_assert(cfg != NULL);
+       g_assert(cfg != nullptr);
        c = cfg->log_format_str;
 
-       if (c == NULL) {
+       if (c == nullptr) {
                return FALSE;
        }
 
@@ -654,12 +611,12 @@ rspamd_config_parse_log_format(struct rspamd_config *cfg)
                case parse_dollar:
                        if (p > c) {
                                /* We have string element that we need to store */
-                               lf = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*lf));
+                               lf = rspamd_mempool_alloc0_type(cfg->cfg_pool, struct rspamd_log_format);
                                lf->type = RSPAMD_LOG_STRING;
                                lf->data = rspamd_mempool_alloc(cfg->cfg_pool, p - c + 1);
                                /* Filter \r\n from the destination */
                                s = c;
-                               d = lf->data;
+                               d = (char *) lf->data;
 
                                while (s < p) {
                                        if (*s != '\r' && *s != '\n') {
@@ -674,7 +631,7 @@ rspamd_config_parse_log_format(struct rspamd_config *cfg)
 
                                lf->len = d - (char *) lf->data;
                                DL_APPEND(cfg->log_format, lf);
-                               lf = NULL;
+                               lf = nullptr;
                        }
                        p++;
                        c = p;
@@ -695,7 +652,7 @@ rspamd_config_parse_log_format(struct rspamd_config *cfg)
                                var.len = p - c;
                                c = p;
 
-                               if (!rspamd_config_process_var(cfg, &var, NULL)) {
+                               if (!rspamd_config_process_var(cfg, &var, nullptr)) {
                                        return FALSE;
                                }
 
@@ -734,12 +691,12 @@ rspamd_config_parse_log_format(struct rspamd_config *cfg)
        case parse_str:
                if (p > c) {
                        /* We have string element that we need to store */
-                       lf = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*lf));
+                       lf = rspamd_mempool_alloc0_type(cfg->cfg_pool, struct rspamd_log_format);
                        lf->type = RSPAMD_LOG_STRING;
                        lf->data = rspamd_mempool_alloc(cfg->cfg_pool, p - c + 1);
                        /* Filter \r\n from the destination */
                        s = c;
-                       d = lf->data;
+                       d = (char *) lf->data;
 
                        while (s < p) {
                                if (*s != '\r' && *s != '\n') {
@@ -754,7 +711,7 @@ rspamd_config_parse_log_format(struct rspamd_config *cfg)
 
                        lf->len = d - (char *) lf->data;
                        DL_APPEND(cfg->log_format, lf);
-                       lf = NULL;
+                       lf = nullptr;
                }
                break;
 
@@ -762,7 +719,7 @@ rspamd_config_parse_log_format(struct rspamd_config *cfg)
                var.begin = c;
                var.len = p - c;
 
-               if (!rspamd_config_process_var(cfg, &var, NULL)) {
+               if (!rspamd_config_process_var(cfg, &var, nullptr)) {
                        return FALSE;
                }
                break;
@@ -783,17 +740,12 @@ rspamd_urls_config_dtor(gpointer _unused)
        rspamd_url_deinit();
 }
 
-/*
- * Perform post load actions
- */
-gboolean
-rspamd_config_post_load(struct rspamd_config *cfg,
-                                               enum rspamd_post_load_options opts)
+static void
+rspamd_adjust_clocks_resolution(struct rspamd_config *cfg)
 {
 #ifdef HAVE_CLOCK_GETTIME
        struct timespec ts;
 #endif
-       gboolean ret = TRUE;
 
 #ifdef HAVE_CLOCK_GETTIME
 #ifdef HAVE_CLOCK_PROCESS_CPUTIME_ID
@@ -803,8 +755,6 @@ rspamd_config_post_load(struct rspamd_config *cfg,
 #else
        clock_getres(CLOCK_REALTIME, &ts);
 #endif
-       rspamd_logger_configure_modules(cfg->debug_modules);
-
        cfg->clock_res = log10(1000000. / ts.tv_nsec);
        if (cfg->clock_res < 0) {
                cfg->clock_res = 0;
@@ -816,6 +766,20 @@ rspamd_config_post_load(struct rspamd_config *cfg,
        /* For gettimeofday */
        cfg->clock_res = 1;
 #endif
+}
+
+/*
+ * Perform post load actions
+ */
+gboolean
+rspamd_config_post_load(struct rspamd_config *cfg,
+                                               enum rspamd_post_load_options opts)
+{
+
+       auto ret = TRUE;
+
+       rspamd_adjust_clocks_resolution(cfg);
+       rspamd_logger_configure_modules(cfg->debug_modules);
 
        if (cfg->one_shot_mode) {
                msg_info_config("enabling one shot mode (was %d max shots)",
@@ -823,9 +787,6 @@ rspamd_config_post_load(struct rspamd_config *cfg,
                cfg->default_max_shots = 1;
        }
 
-       rspamd_regexp_library_init(cfg);
-       rspamd_multipattern_library_init(cfg->hs_cache_dir);
-
 #if defined(WITH_HYPERSCAN) && !defined(__aarch64__) && !defined(__powerpc64__)
        if (!cfg->disable_hyperscan) {
                if (!(cfg->libs_ctx->crypto_ctx->cpu_config & CPUID_SSSE3)) {
@@ -836,20 +797,21 @@ rspamd_config_post_load(struct rspamd_config *cfg,
        }
 #endif
 
+       rspamd_regexp_library_init(cfg);
+       rspamd_multipattern_library_init(cfg->hs_cache_dir);
+
        if (opts & RSPAMD_CONFIG_INIT_URL) {
-               if (cfg->tld_file == NULL) {
+               if (cfg->tld_file == nullptr) {
                        /* Try to guess tld file */
-                       GString *fpath = g_string_new(NULL);
+                       auto fpath = fmt::format("{0}{1}{2}", RSPAMD_SHAREDIR,
+                                                                        G_DIR_SEPARATOR, "effective_tld_names.dat");
 
-                       rspamd_printf_gstring(fpath, "%s%c%s", RSPAMD_SHAREDIR,
-                                                                 G_DIR_SEPARATOR, "effective_tld_names.dat");
-
-                       if (access(fpath->str, R_OK) != -1) {
+                       if (access(fpath.c_str(), R_OK) != -1) {
                                msg_debug_config("url_tld option is not specified but %s is available,"
                                                                 " therefore this file is assumed as TLD file for URL"
                                                                 " extraction",
-                                                                fpath->str);
-                               cfg->tld_file = rspamd_mempool_strdup(cfg->cfg_pool, fpath->str);
+                                                                fpath.c_str());
+                               cfg->tld_file = rspamd_mempool_strdup(cfg->cfg_pool, fpath.c_str());
                        }
                        else {
                                if (opts & RSPAMD_CONFIG_INIT_VALIDATE) {
@@ -857,8 +819,6 @@ rspamd_config_post_load(struct rspamd_config *cfg,
                                        ret = FALSE;
                                }
                        }
-
-                       g_string_free(fpath, TRUE);
                }
                else {
                        if (access(cfg->tld_file, R_OK) == -1) {
@@ -870,20 +830,20 @@ rspamd_config_post_load(struct rspamd_config *cfg,
                                else {
                                        msg_debug_config("cannot access tld file %s: %s", cfg->tld_file,
                                                                         strerror(errno));
-                                       cfg->tld_file = NULL;
+                                       cfg->tld_file = nullptr;
                                }
                        }
                }
 
                if (opts & RSPAMD_CONFIG_INIT_NO_TLD) {
-                       rspamd_url_init(NULL);
+                       rspamd_url_init(nullptr);
                }
                else {
                        rspamd_url_init(cfg->tld_file);
                }
 
                rspamd_mempool_add_destructor(cfg->cfg_pool, rspamd_urls_config_dtor,
-                                                                         NULL);
+                                                                         nullptr);
        }
 
        init_dynamic_config(cfg);
@@ -893,36 +853,41 @@ rspamd_config_post_load(struct rspamd_config *cfg,
        /* Parse format string that we have */
        if (!rspamd_config_parse_log_format(cfg)) {
                msg_err_config("cannot parse log format, task logging will not be available");
+               if (opts & RSPAMD_CONFIG_INIT_VALIDATE) {
+                       ret = FALSE;
+               }
        }
 
        if (opts & RSPAMD_CONFIG_INIT_SYMCACHE) {
                /* Init config cache */
-               rspamd_symcache_init(cfg->cache);
+               ret &= rspamd_symcache_init(cfg->cache);
 
                /* Init re cache */
                rspamd_re_cache_init(cfg->re_cache, cfg);
 
                /* Try load Hypersan */
-               rspamd_re_cache_load_hyperscan(cfg->re_cache,
-                                                                          cfg->hs_cache_dir ? cfg->hs_cache_dir : RSPAMD_DBDIR "/",
-                                                                          true);
+               auto hs_ret = rspamd_re_cache_load_hyperscan(cfg->re_cache,
+                                                                                                        cfg->hs_cache_dir ? cfg->hs_cache_dir : RSPAMD_DBDIR "/",
+                                                                                                        true);
+
+               if (hs_ret == RSPAMD_HYPERSCAN_LOAD_ERROR) {
+                       ret = FALSE;
+               }
        }
 
        if (opts & RSPAMD_CONFIG_INIT_LIBS) {
                /* Config other libraries */
-               rspamd_config_libs(cfg->libs_ctx, cfg);
+               ret &= rspamd_config_libs(cfg->libs_ctx, cfg);
        }
 
        /* Validate cache */
        if (opts & RSPAMD_CONFIG_INIT_VALIDATE) {
                /* Check for actions sanity */
-               gboolean seen_controller = FALSE;
-               GList *cur;
-               struct rspamd_worker_conf *wcf;
+               auto seen_controller = FALSE;
 
-               cur = cfg->workers;
+               auto *cur = cfg->workers;
                while (cur) {
-                       wcf = cur->data;
+                       auto *wcf = (struct rspamd_worker_conf *) cur->data;
 
                        if (wcf->type == g_quark_from_static_string("controller")) {
                                seen_controller = TRUE;
@@ -938,11 +903,11 @@ rspamd_config_post_load(struct rspamd_config *cfg,
                                                        " Rspamd features will be broken");
                }
 
-               ret = rspamd_symcache_validate(cfg->cache, cfg, FALSE) && ret;
+               ret &= rspamd_symcache_validate(cfg->cache, cfg, FALSE);
        }
 
        if (opts & RSPAMD_CONFIG_INIT_POST_LOAD_LUA) {
-               rspamd_lua_run_config_post_init(cfg->lua_state, cfg);
+               rspamd_lua_run_config_post_init(RSPAMD_LUA_CFG_STATE(cfg), cfg);
        }
 
        if (opts & RSPAMD_CONFIG_INIT_PRELOAD_MAPS) {
@@ -956,18 +921,18 @@ struct rspamd_classifier_config *
 rspamd_config_new_classifier(struct rspamd_config *cfg,
                                                         struct rspamd_classifier_config *c)
 {
-       if (c == NULL) {
+       if (c == nullptr) {
                c =
-                       rspamd_mempool_alloc0(cfg->cfg_pool,
-                                                                 sizeof(struct rspamd_classifier_config));
+                       rspamd_mempool_alloc0_type(cfg->cfg_pool,
+                                                                          struct rspamd_classifier_config);
                c->min_prob_strength = 0.05;
                c->min_token_hits = 2;
        }
 
-       if (c->labels == NULL) {
+       if (c->labels == nullptr) {
                c->labels = g_hash_table_new_full(rspamd_str_hash,
                                                                                  rspamd_str_equal,
-                                                                                 NULL,
+                                                                                 nullptr,
                                                                                  (GDestroyNotify) g_list_free);
                rspamd_mempool_add_destructor(cfg->cfg_pool,
                                                                          (rspamd_mempool_destruct_t) g_hash_table_destroy,
@@ -981,10 +946,9 @@ struct rspamd_statfile_config *
 rspamd_config_new_statfile(struct rspamd_config *cfg,
                                                   struct rspamd_statfile_config *c)
 {
-       if (c == NULL) {
+       if (c == nullptr) {
                c =
-                       rspamd_mempool_alloc0(cfg->cfg_pool,
-                                                                 sizeof(struct rspamd_statfile_config));
+                       rspamd_mempool_alloc0_type(cfg->cfg_pool, struct rspamd_statfile_config);
        }
 
        return c;
@@ -1010,7 +974,7 @@ rspamd_config_new_group(struct rspamd_config *cfg, const gchar *name)
 {
        struct rspamd_symbols_group *gr;
 
-       gr = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*gr));
+       gr = rspamd_mempool_alloc0_type(cfg->cfg_pool, struct rspamd_symbols_group);
        gr->symbols = g_hash_table_new(rspamd_strcase_hash,
                                                                   rspamd_strcase_equal);
        rspamd_mempool_add_destructor(cfg->cfg_pool,
@@ -1034,10 +998,7 @@ rspamd_worker_conf_dtor(struct rspamd_worker_conf *wcf)
 
                LL_FOREACH_SAFE(wcf->bind_conf, cnf, tmp)
                {
-                       g_free(cnf->name);
-                       g_free(cnf->bind_line);
                        g_ptr_array_free(cnf->addrs, TRUE);
-                       g_free(cnf);
                }
 
                ucl_object_unref(wcf->options);
@@ -1050,7 +1011,7 @@ rspamd_worker_conf_dtor(struct rspamd_worker_conf *wcf)
 static void
 rspamd_worker_conf_cfg_fin(gpointer d)
 {
-       struct rspamd_worker_conf *wcf = d;
+       auto *wcf = (struct rspamd_worker_conf *) d;
 
        REF_RELEASE(wcf);
 }
@@ -1059,13 +1020,13 @@ struct rspamd_worker_conf *
 rspamd_config_new_worker(struct rspamd_config *cfg,
                                                 struct rspamd_worker_conf *c)
 {
-       if (c == NULL) {
-               c = g_malloc0(sizeof(struct rspamd_worker_conf));
+       if (c == nullptr) {
+               c = g_new0(struct rspamd_worker_conf, 1);
                c->params = g_hash_table_new(rspamd_str_hash, rspamd_str_equal);
                c->active_workers = g_queue_new();
 #ifdef HAVE_SC_NPROCESSORS_ONLN
-               c->count = MIN(DEFAULT_MAX_WORKERS,
-                                          MAX(1, sysconf(_SC_NPROCESSORS_ONLN) - 2));
+               auto nproc = sysconf(_SC_NPROCESSORS_ONLN);
+               c->count = MIN(DEFAULT_MAX_WORKERS, MAX(1, nproc - 2));
 #else
                c->count = DEFAULT_MAX_WORKERS;
 #endif
@@ -1086,18 +1047,13 @@ static bool
 rspamd_include_map_handler(const guchar *data, gsize len,
                                                   const ucl_object_t *args, void *ud)
 {
-       struct rspamd_config *cfg = (struct rspamd_config *) ud;
-       struct rspamd_ucl_map_cbdata *cbdata, **pcbdata;
-       gchar *map_line;
+       auto *cfg = (struct rspamd_config *) ud;
 
-       map_line = rspamd_mempool_alloc(cfg->cfg_pool, len + 1);
-       rspamd_strlcpy(map_line, data, len + 1);
+       auto *map_line = rspamd_mempool_ftokdup(cfg->cfg_pool,
+                                                                                       (rspamd_ftok_t{.len = len + 1, .begin = (char *) data}));
 
-       cbdata = g_malloc(sizeof(struct rspamd_ucl_map_cbdata));
-       pcbdata = g_malloc(sizeof(struct rspamd_ucl_map_cbdata *));
-       cbdata->buf = NULL;
-       cbdata->cfg = cfg;
-       *pcbdata = cbdata;
+       auto *cbdata = new rspamd_ucl_map_cbdata{cfg};
+       auto **pcbdata = new rspamd_ucl_map_cbdata *(cbdata);
 
        return rspamd_map_add(cfg,
                                                  map_line,
@@ -1106,7 +1062,7 @@ rspamd_include_map_handler(const guchar *data, gsize len,
                                                  rspamd_ucl_fin_cb,
                                                  rspamd_ucl_dtor_cb,
                                                  (void **) pcbdata,
-                                                 NULL, RSPAMD_MAP_DEFAULT) != NULL;
+                                                 nullptr, RSPAMD_MAP_DEFAULT) != nullptr;
 }
 
 /*
@@ -1194,7 +1150,7 @@ void rspamd_ucl_add_conf_variables(struct ucl_parser *parser, GHashTable *vars)
        ucl_parser_register_variable(parser, RSPAMD_HOSTNAME_MACRO,
                                                                 hostbuf);
 
-       if (vars != NULL) {
+       if (vars != nullptr) {
                g_hash_table_iter_init(&it, vars);
 
                while (g_hash_table_iter_next(&it, &k, &v)) {
@@ -1218,7 +1174,7 @@ symbols_classifiers_callback(gpointer key, gpointer value, gpointer ud)
        struct rspamd_config *cfg = ud;
 
        /* Actually, statistics should act like any ordinary symbol */
-       rspamd_symcache_add_symbol(cfg->cache, key, 0, NULL, NULL,
+       rspamd_symcache_add_symbol(cfg->cache, key, 0, nullptr, nullptr,
                                                           SYMBOL_TYPE_CLASSIFIER | SYMBOL_TYPE_NOSTAT, -1);
 }
 
@@ -1235,8 +1191,8 @@ rspamd_config_find_classifier(struct rspamd_config *cfg, const gchar *name)
        GList *cur;
        struct rspamd_classifier_config *cf;
 
-       if (name == NULL) {
-               return NULL;
+       if (name == nullptr) {
+               return nullptr;
        }
 
        cur = cfg->classifiers;
@@ -1250,7 +1206,7 @@ rspamd_config_find_classifier(struct rspamd_config *cfg, const gchar *name)
                cur = g_list_next(cur);
        }
 
-       return NULL;
+       return nullptr;
 }
 
 gboolean
@@ -1317,55 +1273,50 @@ rspamd_ucl_read_cb(gchar *chunk,
                                   struct map_cb_data *data,
                                   gboolean final)
 {
-       struct rspamd_ucl_map_cbdata *cbdata = data->cur_data, *prev;
+       auto *cbdata = (struct rspamd_ucl_map_cbdata *) data->cur_data;
+       auto *prev = (struct rspamd_ucl_map_cbdata *) data->prev_data;
 
-       if (cbdata == NULL) {
-               cbdata = g_malloc(sizeof(struct rspamd_ucl_map_cbdata));
-               prev = data->prev_data;
-               cbdata->buf = g_string_sized_new(BUFSIZ);
-               cbdata->cfg = prev->cfg;
+       if (cbdata == nullptr) {
+               cbdata = new rspamd_ucl_map_cbdata{prev->cfg};
                data->cur_data = cbdata;
        }
-       g_string_append_len(cbdata->buf, chunk, len);
+       cbdata->buf.append(chunk, len);
 
        /* Say not to copy any part of this buffer */
-       return NULL;
+       return nullptr;
 }
 
 static void
 rspamd_ucl_fin_cb(struct map_cb_data *data, void **target)
 {
-       struct rspamd_ucl_map_cbdata *cbdata = data->cur_data, *prev =
-                                                                                                                          data->prev_data;
-       ucl_object_t *obj;
-       struct ucl_parser *parser;
-       ucl_object_iter_t it = NULL;
-       const ucl_object_t *cur;
-       struct rspamd_config *cfg = data->map->cfg;
-
-       if (cbdata == NULL) {
-               msg_err_config("map fin error: new data is NULL");
+       auto *cbdata = (struct rspamd_ucl_map_cbdata *) data->cur_data;
+       auto *prev = (struct rspamd_ucl_map_cbdata *) data->prev_data;
+       auto *cfg = data->map->cfg;
+
+       if (cbdata == nullptr) {
+               msg_err_config("map fin error: new data is nullptr");
                return;
        }
 
        /* New data available */
-       parser = ucl_parser_new(0);
-       if (!ucl_parser_add_chunk(parser, cbdata->buf->str,
-                                                         cbdata->buf->len)) {
+       auto *parser = ucl_parser_new(0);
+       if (!ucl_parser_add_chunk(parser, (unsigned char *) cbdata->buf.data(),
+                                                         cbdata->buf.size())) {
                msg_err_config("cannot parse map %s: %s",
                                           data->map->name,
                                           ucl_parser_get_error(parser));
                ucl_parser_free(parser);
        }
        else {
-               obj = ucl_parser_get_object(parser);
-               ucl_parser_free(parser);
-               it = NULL;
+               auto *obj = ucl_parser_get_object(parser);
+               ucl_object_iter_t it = nullptr;
 
-               while ((cur = ucl_object_iterate(obj, &it, true))) {
+               for (auto *cur = ucl_object_iterate(obj, &it, true); cur != nullptr; cur = ucl_object_iterate(obj, &it, true)) {
                        ucl_object_replace_key(cbdata->cfg->rcl_obj, (ucl_object_t *) cur,
                                                                   cur->key, cur->keylen, false);
                }
+
+               ucl_parser_free(parser);
                ucl_object_unref(obj);
        }
 
@@ -1373,25 +1324,15 @@ rspamd_ucl_fin_cb(struct map_cb_data *data, void **target)
                *target = data->cur_data;
        }
 
-       if (prev != NULL) {
-               if (prev->buf != NULL) {
-                       g_string_free(prev->buf, TRUE);
-               }
-               g_free(prev);
-       }
+       delete prev;
 }
 
 static void
 rspamd_ucl_dtor_cb(struct map_cb_data *data)
 {
-       struct rspamd_ucl_map_cbdata *cbdata = data->cur_data;
+       auto *cbdata = (struct rspamd_ucl_map_cbdata *) data->cur_data;
 
-       if (cbdata != NULL) {
-               if (cbdata->buf != NULL) {
-                       g_string_free(cbdata->buf, TRUE);
-               }
-               g_free(cbdata);
-       }
+       delete cbdata;
 }
 
 gboolean
@@ -1399,7 +1340,7 @@ rspamd_check_module(struct rspamd_config *cfg, module_t *mod)
 {
        gboolean ret = TRUE;
 
-       if (mod != NULL) {
+       if (mod != nullptr) {
                if (mod->module_version != RSPAMD_CUR_MODULE_VERSION) {
                        msg_err_config("module %s has incorrect version %xd (%xd expected)",
                                                   mod->name, (gint) mod->module_version, RSPAMD_CUR_MODULE_VERSION);
@@ -1428,7 +1369,7 @@ rspamd_check_worker(struct rspamd_config *cfg, worker_t *wrk)
 {
        gboolean ret = TRUE;
 
-       if (wrk != NULL) {
+       if (wrk != nullptr) {
                if (wrk->worker_version != RSPAMD_CUR_WORKER_VERSION) {
                        msg_err_config("worker %s has incorrect version %xd (%xd expected)",
                                                   wrk->name, wrk->worker_version, RSPAMD_CUR_WORKER_VERSION);
@@ -1463,11 +1404,11 @@ rspamd_init_filters(struct rspamd_config *cfg, bool reconfig, bool strict)
 
        /* Init all compiled modules */
 
-       for (pmod = cfg->compiled_modules; pmod != NULL && *pmod != NULL; pmod++) {
+       for (pmod = cfg->compiled_modules; pmod != nullptr && *pmod != nullptr; pmod++) {
                mod = *pmod;
                if (rspamd_check_module(cfg, mod)) {
                        if (mod->module_init_func(cfg, &mod_ctx) == 0) {
-                               g_assert(mod_ctx != NULL);
+                               g_assert(mod_ctx != nullptr);
                                g_ptr_array_add(cfg->c_modules, mod_ctx);
                                mod_ctx->mod = mod;
                                mod->ctx_offset = i++;
@@ -1480,7 +1421,7 @@ rspamd_init_filters(struct rspamd_config *cfg, bool reconfig, bool strict)
 
        while (cur) {
                /* Perform modules configuring */
-               mod_ctx = NULL;
+               mod_ctx = nullptr;
                PTR_ARRAY_FOREACH(cfg->c_modules, i, cur_ctx)
                {
                        if (g_ascii_strcasecmp(cur_ctx->mod->name,
@@ -1514,7 +1455,7 @@ rspamd_init_filters(struct rspamd_config *cfg, bool reconfig, bool strict)
                        }
                }
 
-               if (mod_ctx == NULL) {
+               if (mod_ctx == nullptr) {
                        msg_warn_config("requested unknown module %s", cur->data);
                }
 
@@ -1533,11 +1474,11 @@ rspamd_config_new_symbol(struct rspamd_config *cfg, const gchar *symbol,
 {
        struct rspamd_symbols_group *sym_group;
        struct rspamd_symbol *sym_def;
-       gdouble *score_ptr;
+       double *score_ptr;
 
        sym_def =
-               rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(struct rspamd_symbol));
-       score_ptr = rspamd_mempool_alloc(cfg->cfg_pool, sizeof(gdouble));
+               rspamd_mempool_alloc0_type(cfg->cfg_pool, struct rspamd_symbol);
+       score_ptr = rspamd_mempool_alloc_type(cfg->cfg_pool, double);
 
        if (isnan(score)) {
                /* In fact, it could be defined later */
@@ -1572,7 +1513,7 @@ rspamd_config_new_symbol(struct rspamd_config *cfg, const gchar *symbol,
        g_hash_table_insert(cfg->symbols, sym_def->name, sym_def);
 
        /* Search for symbol group */
-       if (group == NULL) {
+       if (group == nullptr) {
                group = "ungrouped";
                sym_def->flags |= RSPAMD_SYMBOL_FLAG_UNGROUPED;
        }
@@ -1582,8 +1523,8 @@ rspamd_config_new_symbol(struct rspamd_config *cfg, const gchar *symbol,
                }
        }
 
-       sym_group = g_hash_table_lookup(cfg->groups, group);
-       if (sym_group == NULL) {
+       sym_group = reinterpret_cast<rspamd_symbols_group *>(g_hash_table_lookup(cfg->groups, group));
+       if (sym_group == nullptr) {
                /* Create new group */
                sym_group = rspamd_config_new_group(cfg, group);
        }
@@ -1611,13 +1552,13 @@ rspamd_config_add_symbol(struct rspamd_config *cfg,
        struct rspamd_symbols_group *sym_group;
        guint i;
 
-       g_assert(cfg != NULL);
-       g_assert(symbol != NULL);
+       g_assert(cfg != nullptr);
+       g_assert(symbol != nullptr);
 
-       sym_def = g_hash_table_lookup(cfg->symbols, symbol);
+       sym_def = reinterpret_cast<rspamd_symbol *>(g_hash_table_lookup(cfg->symbols, symbol));
 
-       if (sym_def != NULL) {
-               if (group != NULL) {
+       if (sym_def != nullptr) {
+               if (group != nullptr) {
                        gboolean has_group = FALSE;
 
                        PTR_ARRAY_FOREACH(sym_def->groups, i, sym_group)
@@ -1631,9 +1572,9 @@ rspamd_config_add_symbol(struct rspamd_config *cfg,
 
                        if (!has_group) {
                                /* Non-empty group has a priority over non-grouped one */
-                               sym_group = g_hash_table_lookup(cfg->groups, group);
+                               sym_group = reinterpret_cast<rspamd_symbols_group *>(g_hash_table_lookup(cfg->groups, group));
 
-                               if (sym_group == NULL) {
+                               if (sym_group == nullptr) {
                                        /* Create new group */
                                        sym_group = rspamd_config_new_group(cfg, group);
                                }
@@ -1706,12 +1647,12 @@ rspamd_config_add_symbol(struct rspamd_config *cfg,
 
 
                        /* We also check group information in this case */
-                       if (group != NULL && sym_def->gr != NULL &&
+                       if (group != nullptr && sym_def->gr != nullptr &&
                                strcmp(group, sym_def->gr->name) != 0) {
 
-                               sym_group = g_hash_table_lookup(cfg->groups, group);
+                               sym_group = reinterpret_cast<rspamd_symbols_group *>(g_hash_table_lookup(cfg->groups, group));
 
-                               if (sym_group == NULL) {
+                               if (sym_group == nullptr) {
                                        /* Create new group */
                                        sym_group = rspamd_config_new_group(cfg, group);
                                }
@@ -1745,13 +1686,13 @@ rspamd_config_add_symbol_group(struct rspamd_config *cfg,
        struct rspamd_symbols_group *sym_group;
        guint i;
 
-       g_assert(cfg != NULL);
-       g_assert(symbol != NULL);
-       g_assert(group != NULL);
+       g_assert(cfg != nullptr);
+       g_assert(symbol != nullptr);
+       g_assert(group != nullptr);
 
-       sym_def = g_hash_table_lookup(cfg->symbols, symbol);
+       sym_def = reinterpret_cast<rspamd_symbol *>(g_hash_table_lookup(cfg->symbols, symbol));
 
-       if (sym_def != NULL) {
+       if (sym_def != nullptr) {
                gboolean has_group = FALSE;
 
                PTR_ARRAY_FOREACH(sym_def->groups, i, sym_group)
@@ -1765,9 +1706,9 @@ rspamd_config_add_symbol_group(struct rspamd_config *cfg,
 
                if (!has_group) {
                        /* Non-empty group has a priority over non-grouped one */
-                       sym_group = g_hash_table_lookup(cfg->groups, group);
+                       sym_group = reinterpret_cast<rspamd_symbols_group *>(g_hash_table_lookup(cfg->groups, group));
 
-                       if (sym_group == NULL) {
+                       if (sym_group == nullptr) {
                                /* Create new group */
                                sym_group = rspamd_config_new_group(cfg, group);
                        }
@@ -1791,54 +1732,52 @@ gboolean
 rspamd_config_is_enabled_from_ucl(rspamd_mempool_t *pool,
                                                                  const ucl_object_t *obj)
 {
-       {
-               const ucl_object_t *enabled;
 
-               enabled = ucl_object_lookup(obj, "enabled");
+       const ucl_object_t *enabled;
 
-               if (enabled) {
-                       if (ucl_object_type(enabled) == UCL_BOOLEAN) {
-                               return ucl_object_toboolean(enabled);
-                       }
-                       else if (ucl_object_type(enabled) == UCL_STRING) {
-                               gint ret = rspamd_config_parse_flag(ucl_object_tostring(enabled), 0);
+       enabled = ucl_object_lookup(obj, "enabled");
 
-                               if (ret == 0) {
-                                       return FALSE;
-                               }
-                               else if (ret == -1) {
+       if (enabled) {
+               if (ucl_object_type(enabled) == UCL_BOOLEAN) {
+                       return ucl_object_toboolean(enabled);
+               }
+               else if (ucl_object_type(enabled) == UCL_STRING) {
+                       gint ret = rspamd_config_parse_flag(ucl_object_tostring(enabled), 0);
 
-                                       msg_info_pool_check("wrong value for the `enabled` key");
-                                       return FALSE;
-                               }
-                               /* Default return is TRUE here */
+                       if (ret == 0) {
+                               return FALSE;
                        }
+                       else if (ret == -1) {
+
+                               msg_info_pool_check("wrong value for the `enabled` key");
+                               return FALSE;
+                       }
+                       /* Default return is TRUE here */
                }
        }
 
-       {
-               const ucl_object_t *disabled;
 
-               disabled = ucl_object_lookup(obj, "disabled");
+       const ucl_object_t *disabled;
 
-               if (disabled) {
-                       if (ucl_object_type(disabled) == UCL_BOOLEAN) {
-                               return !ucl_object_toboolean(disabled);
-                       }
-                       else if (ucl_object_type(disabled) == UCL_STRING) {
-                               gint ret = rspamd_config_parse_flag(ucl_object_tostring(disabled), 0);
+       disabled = ucl_object_lookup(obj, "disabled");
 
-                               if (ret == 0) {
-                                       return TRUE;
-                               }
-                               else if (ret == -1) {
+       if (disabled) {
+               if (ucl_object_type(disabled) == UCL_BOOLEAN) {
+                       return !ucl_object_toboolean(disabled);
+               }
+               else if (ucl_object_type(disabled) == UCL_STRING) {
+                       gint ret = rspamd_config_parse_flag(ucl_object_tostring(disabled), 0);
 
-                                       msg_info_pool_check("wrong value for the `disabled` key");
-                                       return FALSE;
-                               }
+                       if (ret == 0) {
+                               return TRUE;
+                       }
+                       else if (ret == -1) {
 
+                               msg_info_pool_check("wrong value for the `disabled` key");
                                return FALSE;
                        }
+
+                       return FALSE;
                }
        }
 
@@ -1853,7 +1792,7 @@ rspamd_config_is_module_enabled(struct rspamd_config *cfg,
        const ucl_object_t *conf;
        GList *cur;
        struct rspamd_symbols_group *gr;
-       lua_State *L = cfg->lua_state;
+       lua_State *L = RSPAMD_LUA_CFG_STATE(cfg);
        struct module_ctx *cur_ctx;
        guint i;
 
@@ -1865,7 +1804,7 @@ rspamd_config_is_module_enabled(struct rspamd_config *cfg,
                }
        }
 
-       if (g_hash_table_lookup(cfg->explicit_modules, module_name) != NULL) {
+       if (g_hash_table_lookup(cfg->explicit_modules, module_name) != nullptr) {
                /* Always load module */
                rspamd_plugins_table_push_elt(L, "enabled", module_name);
 
@@ -1878,7 +1817,7 @@ rspamd_config_is_module_enabled(struct rspamd_config *cfg,
                cur = g_list_first(cfg->filters);
 
                while (cur) {
-                       if (strcmp(cur->data, module_name) == 0) {
+                       if (strcmp((char *) cur->data, module_name) == 0) {
                                found = TRUE;
                                break;
                        }
@@ -1898,7 +1837,7 @@ rspamd_config_is_module_enabled(struct rspamd_config *cfg,
 
        conf = ucl_object_lookup(cfg->rcl_obj, module_name);
 
-       if (conf == NULL) {
+       if (conf == nullptr) {
                rspamd_plugins_table_push_elt(L, "disabled_unconfigured", module_name);
 
                msg_info_config("%s module %s is enabled but has not been configured",
@@ -1924,7 +1863,7 @@ rspamd_config_is_module_enabled(struct rspamd_config *cfg,
        }
 
        /* Now we check symbols group */
-       gr = g_hash_table_lookup(cfg->groups, module_name);
+       gr = reinterpret_cast<rspamd_symbols_group *>(g_hash_table_lookup(cfg->groups, module_name));
 
        if (gr) {
                if (gr->flags & RSPAMD_SYMBOL_GROUP_DISABLED) {
@@ -1958,7 +1897,7 @@ rspamd_config_action_from_ucl(struct rspamd_config *cfg,
        if (obj_type == UCL_OBJECT) {
                obj_type = ucl_object_type(obj);
 
-               elt = ucl_object_lookup_any(obj, "score", "threshold", NULL);
+               elt = ucl_object_lookup_any(obj, "score", "threshold", nullptr);
 
                if (elt) {
                        threshold = ucl_object_todouble(elt);
@@ -1968,9 +1907,9 @@ rspamd_config_action_from_ucl(struct rspamd_config *cfg,
 
                if (elt && ucl_object_type(elt) == UCL_ARRAY) {
                        const ucl_object_t *cur;
-                       ucl_object_iter_t it = NULL;
+                       ucl_object_iter_t it = nullptr;
 
-                       while ((cur = ucl_object_iterate(elt, &it, true)) != NULL) {
+                       while ((cur = ucl_object_iterate(elt, &it, true)) != nullptr) {
                                if (ucl_object_type(cur) == UCL_STRING) {
                                        const gchar *fl_str = ucl_object_tostring(cur);
 
@@ -2047,8 +1986,8 @@ rspamd_config_set_action_score(struct rspamd_config *cfg,
        const ucl_object_t *elt;
        guint priority = ucl_object_get_priority(obj), obj_type;
 
-       g_assert(cfg != NULL);
-       g_assert(action_name != NULL);
+       g_assert(cfg != nullptr);
+       g_assert(action_name != nullptr);
 
        obj_type = ucl_object_type(obj);
 
@@ -2154,7 +2093,7 @@ rspamd_config_maybe_disable_action(struct rspamd_config *cfg,
 struct rspamd_action *
 rspamd_config_get_action(struct rspamd_config *cfg, const gchar *name)
 {
-       struct rspamd_action *res = NULL;
+       struct rspamd_action *res = nullptr;
 
        HASH_FIND_STR(cfg->actions, name, res);
 
@@ -2174,7 +2113,7 @@ rspamd_config_get_action_by_type(struct rspamd_config *cfg,
                }
        }
 
-       return NULL;
+       return nullptr;
 }
 
 gboolean
@@ -2183,12 +2122,12 @@ rspamd_config_radix_from_ucl(struct rspamd_config *cfg, const ucl_object_t *obj,
                                                         struct rspamd_worker *worker, const gchar *map_name)
 {
        ucl_type_t type;
-       ucl_object_iter_t it = NULL;
+       ucl_object_iter_t it = nullptr;
        const ucl_object_t *cur, *cur_elt;
        const gchar *str;
 
        /* Cleanup */
-       *target = NULL;
+       *target = nullptr;
 
        LL_FOREACH(obj, cur_elt)
        {
@@ -2206,7 +2145,7 @@ rspamd_config_radix_from_ucl(struct rspamd_config *cfg, const ucl_object_t *obj,
                                                                                        rspamd_radix_fin,
                                                                                        rspamd_radix_dtor,
                                                                                        (void **) target,
-                                                                                       worker, RSPAMD_MAP_DEFAULT) == NULL) {
+                                                                                       worker, RSPAMD_MAP_DEFAULT) == nullptr) {
                                        g_set_error(err,
                                                                g_quark_from_static_string("rspamd-config"),
                                                                EINVAL, "bad map definition %s for %s", str,
@@ -2234,7 +2173,7 @@ rspamd_config_radix_from_ucl(struct rspamd_config *cfg, const ucl_object_t *obj,
                                                                                rspamd_radix_fin,
                                                                                rspamd_radix_dtor,
                                                                                (void **) target,
-                                                                               worker, RSPAMD_MAP_DEFAULT) == NULL) {
+                                                                               worker, RSPAMD_MAP_DEFAULT) == nullptr) {
                                g_set_error(err,
                                                        g_quark_from_static_string("rspamd-config"),
                                                        EINVAL, "bad map object for %s", ucl_object_key(obj));
@@ -2247,7 +2186,7 @@ rspamd_config_radix_from_ucl(struct rspamd_config *cfg, const ucl_object_t *obj,
                        /* List of IP addresses */
                        it = ucl_object_iterate_new(cur_elt);
 
-                       while ((cur = ucl_object_iterate_safe(it, true)) != NULL) {
+                       while ((cur = ucl_object_iterate_safe(it, true)) != nullptr) {
 
 
                                if (ucl_object_type(cur) == UCL_STRING) {
@@ -2457,7 +2396,7 @@ rspamd_config_find_settings_id_ref(struct rspamd_config *cfg,
                }
        }
 
-       return NULL;
+       return nullptr;
 }
 
 struct rspamd_config_settings_elt *rspamd_config_find_settings_name_ref(
@@ -2543,7 +2482,7 @@ void rspamd_config_register_settings_id(struct rspamd_config *cfg,
 int rspamd_config_ev_backend_get(struct rspamd_config *cfg)
 {
 #define AUTO_BACKEND (ev_supported_backends() & ~EVBACKEND_IOURING)
-       if (cfg == NULL || cfg->events_backend == NULL) {
+       if (cfg == nullptr || cfg->events_backend == nullptr) {
                return AUTO_BACKEND;
        }
 
@@ -2597,9 +2536,9 @@ int rspamd_config_ev_backend_get(struct rspamd_config *cfg)
 const gchar *
 rspamd_config_ev_backend_to_string(int ev_backend, gboolean *effective)
 {
-#define SET_EFFECTIVE(b)                           \
-       do {                                           \
-               if ((effective) != NULL) *(effective) = b; \
+#define SET_EFFECTIVE(b)                              \
+       do {                                              \
+               if ((effective) != nullptr) *(effective) = b; \
        } while (0)
 
        if ((ev_backend & EVBACKEND_ALL) == EVBACKEND_ALL) {
@@ -2665,7 +2604,7 @@ rspamd_init_libs(void)
                ottery_config_disable_entropy_sources(ottery_cfg,
                                                                                          OTTERY_ENTROPY_SRC_RDRAND);
 #if OPENSSL_VERSION_NUMBER >= 0x1000104fL && OPENSSL_VERSION_NUMBER < 0x30000000L && !defined(LIBRESSL_VERSION_NUMBER)
-               RAND_set_rand_engine(NULL);
+               RAND_set_rand_engine(nullptr);
 #endif
        }
 
@@ -2684,7 +2623,7 @@ rspamd_init_libs(void)
        g_assert(ottery_init(ottery_cfg) == 0);
 
 #ifdef HAVE_LOCALE_H
-       if (getenv("LANG") == NULL) {
+       if (getenv("LANG") == nullptr) {
                setlocale(LC_ALL, "C");
                setlocale(LC_CTYPE, "C");
                setlocale(LC_MESSAGES, "C");
@@ -2722,10 +2661,10 @@ rspamd_open_zstd_dictionary(const char *path)
        dict = g_malloc0(sizeof(*dict));
        dict->dict = rspamd_file_xmap(path, PROT_READ, &dict->size, TRUE);
 
-       if (dict->dict == NULL) {
+       if (dict->dict == nullptr) {
                g_free(dict);
 
-               return NULL;
+               return nullptr;
        }
 
        dict->id = -1;
@@ -2733,7 +2672,7 @@ rspamd_open_zstd_dictionary(const char *path)
        if (dict->id == 0) {
                g_free(dict);
 
-               return NULL;
+               return nullptr;
        }
 
        return dict;
@@ -2762,15 +2701,15 @@ rspamd_config_libs(struct rspamd_external_libs_ctx *ctx,
        size_t r;
        gboolean ret = TRUE;
 
-       g_assert(cfg != NULL);
+       g_assert(cfg != nullptr);
 
-       if (ctx != NULL) {
+       if (ctx != nullptr) {
                if (cfg->local_addrs) {
                        ret = rspamd_config_radix_from_ucl(cfg, cfg->local_addrs,
                                                                                           "Local addresses",
                                                                                           (struct rspamd_radix_map_helper **) ctx->local_addrs,
-                                                                                          NULL,
-                                                                                          NULL, "local addresses");
+                                                                                          nullptr,
+                                                                                          nullptr, "local addresses");
                }
 
                rspamd_free_zstd_dictionary(ctx->in_dict);
@@ -2778,19 +2717,19 @@ rspamd_config_libs(struct rspamd_external_libs_ctx *ctx,
 
                if (ctx->out_zstream) {
                        ZSTD_freeCStream(ctx->out_zstream);
-                       ctx->out_zstream = NULL;
+                       ctx->out_zstream = nullptr;
                }
 
                if (ctx->in_zstream) {
                        ZSTD_freeDStream(ctx->in_zstream);
-                       ctx->in_zstream = NULL;
+                       ctx->in_zstream = nullptr;
                }
 
                if (cfg->zstd_input_dictionary) {
                        ctx->in_dict = rspamd_open_zstd_dictionary(
                                cfg->zstd_input_dictionary);
 
-                       if (ctx->in_dict == NULL) {
+                       if (ctx->in_dict == nullptr) {
                                msg_err_config("cannot open zstd dictionary in %s",
                                                           cfg->zstd_input_dictionary);
                        }
@@ -2799,7 +2738,7 @@ rspamd_config_libs(struct rspamd_external_libs_ctx *ctx,
                        ctx->out_dict = rspamd_open_zstd_dictionary(
                                cfg->zstd_output_dictionary);
 
-                       if (ctx->out_dict == NULL) {
+                       if (ctx->out_dict == nullptr) {
                                msg_err_config("cannot open zstd dictionary in %s",
                                                           cfg->zstd_output_dictionary);
                        }
@@ -2813,7 +2752,7 @@ rspamd_config_libs(struct rspamd_external_libs_ctx *ctx,
                        /* Toggle FIPS mode */
                        if (mode == 0) {
 #if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR >= 3)
-                               if (EVP_set_default_properties(NULL, "fips=yes") != 1) {
+                               if (EVP_set_default_properties(nullptr, "fips=yes") != 1) {
 #else
                                if (FIPS_mode_set(1) != 1) {
 #endif
@@ -2830,7 +2769,7 @@ rspamd_config_libs(struct rspamd_external_libs_ctx *ctx,
 #else
                                msg_err_config("FIPS_mode_set failed: %s",
 #endif
-                                                          ERR_error_string(err, NULL));
+                                                          ERR_error_string(err, nullptr));
                                ret = FALSE;
                        }
                        else {
@@ -2852,7 +2791,7 @@ rspamd_config_libs(struct rspamd_external_libs_ctx *ctx,
                        msg_err("cannot init decompression stream: %s",
                                        ZSTD_getErrorName(r));
                        ZSTD_freeDStream(ctx->in_zstream);
-                       ctx->in_zstream = NULL;
+                       ctx->in_zstream = nullptr;
                }
 
                /* Init compression */
@@ -2863,7 +2802,7 @@ rspamd_config_libs(struct rspamd_external_libs_ctx *ctx,
                        msg_err("cannot init compression stream: %s",
                                        ZSTD_getErrorName(r));
                        ZSTD_freeCStream(ctx->out_zstream);
-                       ctx->out_zstream = NULL;
+                       ctx->out_zstream = nullptr;
                }
 #ifdef HAVE_OPENBLAS_SET_NUM_THREADS
                openblas_set_num_threads(cfg->max_blas_threads);
@@ -2881,7 +2820,7 @@ rspamd_libs_reset_decompression(struct rspamd_external_libs_ctx *ctx)
 {
        gsize r;
 
-       if (ctx->in_zstream == NULL) {
+       if (ctx->in_zstream == nullptr) {
                return FALSE;
        }
        else {
@@ -2891,7 +2830,7 @@ rspamd_libs_reset_decompression(struct rspamd_external_libs_ctx *ctx)
                        msg_err("cannot init decompression stream: %s",
                                        ZSTD_getErrorName(r));
                        ZSTD_freeDStream(ctx->in_zstream);
-                       ctx->in_zstream = NULL;
+                       ctx->in_zstream = nullptr;
 
                        return FALSE;
                }
@@ -2905,7 +2844,7 @@ rspamd_libs_reset_compression(struct rspamd_external_libs_ctx *ctx)
 {
        gsize r;
 
-       if (ctx->out_zstream == NULL) {
+       if (ctx->out_zstream == nullptr) {
                return FALSE;
        }
        else {
@@ -2919,7 +2858,7 @@ rspamd_libs_reset_compression(struct rspamd_external_libs_ctx *ctx)
                        msg_err("cannot init compression stream: %s",
                                        ZSTD_getErrorName(r));
                        ZSTD_freeCStream(ctx->out_zstream);
-                       ctx->out_zstream = NULL;
+                       ctx->out_zstream = nullptr;
 
                        return FALSE;
                }
@@ -2930,7 +2869,7 @@ rspamd_libs_reset_compression(struct rspamd_external_libs_ctx *ctx)
 
 void rspamd_deinit_libs(struct rspamd_external_libs_ctx *ctx)
 {
-       if (ctx != NULL) {
+       if (ctx != nullptr) {
                g_free(ctx->ottery_cfg);
 
 #ifdef HAVE_OPENSSL
@@ -2961,7 +2900,7 @@ gboolean
 rspamd_ip_is_local_cfg(struct rspamd_config *cfg,
                                           const rspamd_inet_addr_t *addr)
 {
-       struct rspamd_radix_map_helper *local_addrs = NULL;
+       struct rspamd_radix_map_helper *local_addrs = nullptr;
 
        if (cfg && cfg->libs_ctx) {
                local_addrs = *(struct rspamd_radix_map_helper **) cfg->libs_ctx->local_addrs;
@@ -2972,7 +2911,7 @@ rspamd_ip_is_local_cfg(struct rspamd_config *cfg,
        }
 
        if (local_addrs) {
-               if (rspamd_match_radix_map_addr(local_addrs, addr) != NULL) {
+               if (rspamd_match_radix_map_addr(local_addrs, addr) != nullptr) {
                        return TRUE;
                }
        }
index f5771ff7a1d02691c6a4f09d7c90f9fc9a50d907..a348bcaaa77144b7225fa6b6301491785a62ce32 100644 (file)
@@ -1,11 +1,11 @@
-/*-
- * Copyright 2016 Vsevolod Stakhov
+/*
+ * Copyright 2023 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -621,7 +621,7 @@ rspamd_mempool_strdup_(rspamd_mempool_t *pool, const gchar *src, const gchar *lo
 }
 
 gchar *
-rspamd_mempool_ftokdup_(rspamd_mempool_t *pool, const rspamd_ftok_t *src,
+rspamd_mempool_ftokdup_(rspamd_mempool_t *pool, struct f_str_tok src,
                                                const gchar *loc)
 {
        gchar *newstr;
index 23f70092f527329426541cad6afa227f49f12d22..8c253efe7a69eb9a4e7e6c792840e55679dd73ad 100644 (file)
@@ -1,11 +1,11 @@
-/*-
- * Copyright 2019 Vsevolod Stakhov
+/*
+ * Copyright 2023 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -226,7 +226,7 @@ struct f_str_tok;
  * @return pointer to newly created string that is copy of src
  */
 gchar *rspamd_mempool_ftokdup_(rspamd_mempool_t *pool,
-                                                          const struct f_str_tok *src,
+                                                          struct f_str_tok src,
                                                           const gchar *loc)
        RSPAMD_ATTR_ALLOC_ALIGN(MIN_MEM_ALIGNMENT);
 #define rspamd_mempool_ftokdup(pool, src) \
index bf887e30134b73cb1de69f4e108e350d55dd4a47..32b61dcf86d174e80926d3ebe06555542ef07b49 100644 (file)
@@ -97,7 +97,7 @@ static inline int lua_absindex(lua_State *L, int i)
 
 extern const luaL_reg null_reg[];
 
-#define RSPAMD_LUA_API_VERSION 12
+#define RSPAMD_LUA_CFG_STATE(cfg) ((lua_State *) ((cfg)->lua_state))
 /**
 * Lua IP address structure
 */