commit dce02c4: [Project] Move action definition to avoid uthash pollution

Vsevolod Stakhov vsevolod at highsecure.ru
Mon Jan 14 18:00:07 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-01-14 09:58:18 +0000
URL: https://github.com/rspamd/rspamd/commit/dce02c48d87886154ce05285f4000c371cd1e26f

[Project] Move action definition to avoid uthash pollution

---
 src/libserver/cfg_file.h                           | 15 ++-------
 .../siphash.h => libserver/cfg_file_private.h}     | 38 ++++++++++++----------
 src/libserver/cfg_rcl.c                            |  4 +--
 src/libserver/cfg_utils.c                          | 29 +++--------------
 src/lua/lua_config.c                               | 13 ++++----
 5 files changed, 36 insertions(+), 63 deletions(-)

diff --git a/src/libserver/cfg_file.h b/src/libserver/cfg_file.h
index 3a8094585..1a30d3889 100644
--- a/src/libserver/cfg_file.h
+++ b/src/libserver/cfg_file.h
@@ -279,19 +279,8 @@ enum rspamd_action_flags {
 	RSPAMD_ACTION_HAM = (1u << 2),
 };
 
-struct UT_hash_handle;
-/**
- * Action config definition
- */
-struct rspamd_action {
-	enum rspamd_action_type action;
-	enum rspamd_action_flags flags;
-	guint priority;
-	gint lua_handler_ref; /* If special handling is needed */
-	gdouble threshold;
-	gchar *name;
-	struct UT_hash_handle hh; /* Index by name */
-};
+
+struct rspamd_action;
 
 struct rspamd_config_post_load_script {
 	gint cbref;
diff --git a/src/libcryptobox/siphash/siphash.h b/src/libserver/cfg_file_private.h
similarity index 53%
copy from src/libcryptobox/siphash/siphash.h
copy to src/libserver/cfg_file_private.h
index c6ceb5fb3..03f735efe 100644
--- a/src/libcryptobox/siphash/siphash.h
+++ b/src/libserver/cfg_file_private.h
@@ -1,5 +1,5 @@
 /*-
- * Copyright 2016 Vsevolod Stakhov
+ * Copyright 2019 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -13,22 +13,26 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#ifndef SIPHASH_H_
-#define SIPHASH_H_
 
-#include <stddef.h>
+#ifndef RSPAMD_CFG_FILE_PRIVATE_H
+#define RSPAMD_CFG_FILE_PRIVATE_H
 
-#if defined(__cplusplus)
-extern "C"
-{
-#endif
-const char* siphash_load (void);
-void siphash24 (unsigned char *out,
-		const unsigned char *in,
-		unsigned long long inlen,
-		const unsigned char *k);
-#if defined(__cplusplus)
-}
-#endif
+#include "cfg_file.h"
+#include "../../contrib/mumhash/mum.h"
+#define HASH_CASELESS
+#include "uthash_strcase.h"
 
-#endif /* SIPHASH_H_ */
+/**
+ * Action config definition
+ */
+struct rspamd_action {
+	enum rspamd_action_type action;
+	enum rspamd_action_flags flags;
+	guint priority;
+	gint lua_handler_ref; /* If special handling is needed */
+	gdouble threshold;
+	gchar *name;
+	struct UT_hash_handle hh; /* Index by name */
+};
+
+#endif
diff --git a/src/libserver/cfg_rcl.c b/src/libserver/cfg_rcl.c
index bb807d932..c03af9c71 100644
--- a/src/libserver/cfg_rcl.c
+++ b/src/libserver/cfg_rcl.c
@@ -15,9 +15,7 @@
  */
 #include "cfg_rcl.h"
 #include "rspamd.h"
-#include "../../contrib/mumhash/mum.h"
-#define HASH_CASELESS
-#include "uthash_strcase.h"
+#include "cfg_file_private.h"
 #include "utlist.h"
 #include "cfg_file.h"
 #include "lua/lua_common.h"
diff --git a/src/libserver/cfg_utils.c b/src/libserver/cfg_utils.c
index 5e3193cba..eda78277d 100644
--- a/src/libserver/cfg_utils.c
+++ b/src/libserver/cfg_utils.c
@@ -17,9 +17,7 @@
 
 #include "cfg_file.h"
 #include "rspamd.h"
-#include "../../contrib/mumhash/mum.h"
-#define HASH_CASELESS
-#include "uthash_strcase.h"
+#include "cfg_file_private.h"
 #include "filter.h"
 #include "lua/lua_common.h"
 #include "lua/lua_thread_pool.h"
@@ -848,29 +846,10 @@ rspamd_config_post_load (struct rspamd_config *cfg,
 	/* Validate cache */
 	if (opts & RSPAMD_CONFIG_INIT_VALIDATE) {
 		/* Check for actions sanity */
-		int i, prev_act = 0;
-		gdouble prev_score = NAN;
 		gboolean seen_controller = FALSE;
 		GList *cur;
 		struct rspamd_worker_conf *wcf;
 
-		for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i ++) {
-			if (!isnan (prev_score) && !isnan (cfg->actions[i].threshold)) {
-				if (prev_score <= isnan (cfg->actions[i].threshold)) {
-					msg_warn_config ("incorrect metrics scores: action %s"
-							" has lower score: %.2f than action %s: %.2f",
-							rspamd_action_to_str (prev_act), prev_score,
-							rspamd_action_to_str (i), cfg->actions[i].threshold);
-					ret = FALSE;
-				}
-			}
-
-			if (!isnan (cfg->actions[i].threshold)) {
-				prev_score = cfg->actions[i].score;
-				prev_act = i;
-			}
-		}
-
 		cur = cfg->workers;
 		while (cur) {
 			wcf = cur->data;
@@ -1034,8 +1013,6 @@ rspamd_config_new_statfile (struct rspamd_config *cfg,
 void
 rspamd_config_init_metric (struct rspamd_config *cfg)
 {
-	int i;
-
 	cfg->grow_factor = 1.0;
 	cfg->symbols = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
 	cfg->groups = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal);
@@ -2195,6 +2172,8 @@ rspamd_action_to_str (enum rspamd_action_type action)
 		return "no action";
 	case METRIC_ACTION_MAX:
 		return "invalid max action";
+	case METRIC_ACTION_CUSTOM:
+		return "custom";
 	}
 
 	return "unknown action";
@@ -2218,6 +2197,8 @@ rspamd_action_to_str_alt (enum rspamd_action_type action)
 		return "no action";
 	case METRIC_ACTION_MAX:
 		return "invalid max action";
+	case METRIC_ACTION_CUSTOM:
+		return "custom";
 	}
 
 	return "unknown action";
diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c
index 0b9811272..1b7eaaa4d 100644
--- a/src/lua/lua_config.c
+++ b/src/lua/lua_config.c
@@ -17,6 +17,7 @@
 #include "libmime/message.h"
 #include "libutil/expression.h"
 #include "libserver/composites.h"
+#include "libserver/cfg_file_private.h"
 #include "libmime/lang_detection.h"
 #include "lua/lua_map.h"
 #include "lua/lua_thread_pool.h"
@@ -2220,15 +2221,15 @@ lua_config_get_all_actions (lua_State * L)
 {
 	LUA_TRACE_POINT;
 	struct rspamd_config *cfg = lua_check_config (L, 1);
-	gint act = 0;
+	struct rspamd_action *act, *tmp;
 
 	if (cfg) {
-		lua_newtable (L);
+		lua_createtable (L, 0, HASH_COUNT (cfg->actions));
 
-		for (act = METRIC_ACTION_REJECT; act < METRIC_ACTION_MAX; act ++) {
-			if (!isnan (cfg->actions[act].threshold)) {
-				lua_pushstring (L, rspamd_action_to_str (act));
-				lua_pushnumber (L, cfg->actions[act].threshold);
+		HASH_ITER (hh, cfg->actions, act, tmp) {
+			if (!isnan (act->threshold)) {
+				lua_pushstring (L, act->name);
+				lua_pushnumber (L, act->threshold);
 				lua_settable (L, -3);
 			}
 		}


More information about the Commits mailing list