commit 6c76c17: [Minor] Strictly check worker context magic

Vsevolod Stakhov vsevolod at highsecure.ru
Fri Oct 4 14:14:07 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-10-04 13:38:10 +0100
URL: https://github.com/rspamd/rspamd/commit/6c76c1728c81e25be854e36cd9def1d689536b27

[Minor] Strictly check worker context magic

---
 src/controller.c            | 1 +
 src/fuzzy_storage.c         | 1 +
 src/hs_helper.c             | 1 +
 src/libserver/worker_util.c | 8 ++++++++
 src/libserver/worker_util.h | 8 ++++++++
 src/rspamd_proxy.c          | 1 +
 src/worker.c                | 2 +-
 7 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/src/controller.c b/src/controller.c
index 27c6d18cc..332737724 100644
--- a/src/controller.c
+++ b/src/controller.c
@@ -3701,6 +3701,7 @@ start_controller_worker (struct rspamd_worker *worker)
 	const ev_tstamp save_stats_interval = 60; /* 1 minute */
 	gpointer m;
 
+	g_assert (rspamd_worker_check_context (worker->ctx, rspamd_controller_ctx_magic));
 	ctx->event_loop = rspamd_prepare_worker (worker,
 			"controller",
 			rspamd_controller_accept_socket);
diff --git a/src/fuzzy_storage.c b/src/fuzzy_storage.c
index ef2afac4b..d0d69c83c 100644
--- a/src/fuzzy_storage.c
+++ b/src/fuzzy_storage.c
@@ -1895,6 +1895,7 @@ start_fuzzy (struct rspamd_worker *worker)
 	struct rspamd_srv_command srv_cmd;
 	struct rspamd_config *cfg = worker->srv->cfg;
 
+	g_assert (rspamd_worker_check_context (worker->ctx, rspamd_fuzzy_storage_magic));
 	ctx->event_loop = rspamd_prepare_worker (worker,
 			"fuzzy",
 			NULL);
diff --git a/src/hs_helper.c b/src/hs_helper.c
index 3cdc2a439..a20e176f6 100644
--- a/src/hs_helper.c
+++ b/src/hs_helper.c
@@ -303,6 +303,7 @@ start_hs_helper (struct rspamd_worker *worker)
 	struct hs_helper_ctx *ctx = worker->ctx;
 	double tim;
 
+	g_assert (rspamd_worker_check_context (worker->ctx, rspamd_hs_helper_magic));
 	ctx->cfg = worker->srv->cfg;
 
 	if (ctx->hs_dir == NULL) {
diff --git a/src/libserver/worker_util.c b/src/libserver/worker_util.c
index 4779fd57a..eff39a8b0 100644
--- a/src/libserver/worker_util.c
+++ b/src/libserver/worker_util.c
@@ -1472,4 +1472,12 @@ rspamd_worker_hyperscan_ready (struct rspamd_main *rspamd_main,
 
 	return TRUE;
 }
+
+gboolean
+rspamd_worker_check_context (gpointer ctx, guint64 magic)
+{
+	struct rspamd_abstract_worker_ctx *actx = (struct rspamd_abstract_worker_ctx*)ctx;
+
+	return actx->magic == magic;
+}
 #endif
\ No newline at end of file
diff --git a/src/libserver/worker_util.h b/src/libserver/worker_util.h
index d0966e009..6fbda0b4a 100644
--- a/src/libserver/worker_util.h
+++ b/src/libserver/worker_util.h
@@ -57,6 +57,14 @@ struct ev_loop *
 rspamd_prepare_worker (struct rspamd_worker *worker, const char *name,
 					   rspamd_accept_handler hdl);
 
+/**
+ * Should be used to validate context for a worker as in assert like invocation
+ * @param ctx
+ * @param magic
+ * @return
+ */
+gboolean rspamd_worker_check_context (gpointer ctx, guint64 magic);
+
 /**
  * Set special signal handler for a worker
  */
diff --git a/src/rspamd_proxy.c b/src/rspamd_proxy.c
index be3395273..dbf905609 100644
--- a/src/rspamd_proxy.c
+++ b/src/rspamd_proxy.c
@@ -2216,6 +2216,7 @@ start_rspamd_proxy (struct rspamd_worker *worker)
 {
 	struct rspamd_proxy_ctx *ctx = worker->ctx;
 
+	g_assert (rspamd_worker_check_context (worker->ctx, rspamd_rspamd_proxy_magic));
 	ctx->cfg = worker->srv->cfg;
 	ctx->event_loop = rspamd_prepare_worker (worker, "rspamd_proxy",
 			proxy_accept_socket);
diff --git a/src/worker.c b/src/worker.c
index 349a0ea92..04447feea 100644
--- a/src/worker.c
+++ b/src/worker.c
@@ -525,7 +525,6 @@ init_worker (struct rspamd_config *cfg)
 	GQuark type;
 
 	type = g_quark_try_string ("normal");
-
 	ctx = rspamd_mempool_alloc0 (cfg->cfg_pool,
 			sizeof (struct rspamd_worker_ctx));
 
@@ -645,6 +644,7 @@ start_worker (struct rspamd_worker *worker)
 {
 	struct rspamd_worker_ctx *ctx = worker->ctx;
 
+	g_assert (rspamd_worker_check_context (worker->ctx, rspamd_worker_magic));
 	ctx->cfg = worker->srv->cfg;
 	ctx->event_loop = rspamd_prepare_worker (worker, "normal", accept_socket);
 	rspamd_symcache_start_refresh (worker->srv->cfg->cache, ctx->event_loop,


More information about the Commits mailing list