commit 3da3ad2: [Minor] Further static leaks removal

Vsevolod Stakhov vsevolod at highsecure.ru
Fri Sep 27 14:35:07 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-09-27 15:28:07 +0100
URL: https://github.com/rspamd/rspamd/commit/3da3ad2506b2326ca533b11b9ab8e2cebb92b63f (HEAD -> master)

[Minor] Further static leaks removal

---
 src/controller.c             |  5 +++--
 src/libcryptobox/cryptobox.c |  9 +++++++++
 src/libcryptobox/cryptobox.h |  1 +
 src/libserver/cfg_utils.c    | 10 ++++++----
 src/libutil/http_context.c   |  5 +----
 src/libutil/logger.c         | 20 +++++++++++++++++---
 src/libutil/regexp.c         |  1 +
 src/libutil/util.c           |  2 ++
 src/rspamadm/configtest.c    |  6 +++++-
 src/rspamadm/rspamadm.c      |  5 ++++-
 src/rspamd_proxy.c           |  5 +++--
 src/worker.c                 |  5 +++--
 12 files changed, 55 insertions(+), 19 deletions(-)

diff --git a/src/controller.c b/src/controller.c
index 2f76bcc69..474e7ec92 100644
--- a/src/controller.c
+++ b/src/controller.c
@@ -3769,6 +3769,9 @@ start_controller_worker (struct rspamd_worker *worker)
 	/* Accept event */
 	ctx->http_ctx = rspamd_http_context_create (ctx->cfg, ctx->event_loop,
 			ctx->cfg->ups_ctx);
+	rspamd_mempool_add_destructor (ctx->cfg->cfg_pool,
+			(rspamd_mempool_destruct_t)rspamd_http_context_free,
+			ctx->http_ctx);
 	ctx->http = rspamd_http_router_new (rspamd_controller_error_handler,
 			rspamd_controller_finish_handler, ctx->timeout,
 			ctx->static_files_dir, ctx->http_ctx);
@@ -3942,9 +3945,7 @@ start_controller_worker (struct rspamd_worker *worker)
 	g_hash_table_unref (ctx->plugins);
 	g_hash_table_unref (ctx->custom_commands);
 
-	struct rspamd_http_context *http_ctx = ctx->http_ctx;
 	REF_RELEASE (ctx->cfg);
-	rspamd_http_context_free (http_ctx);
 	rspamd_log_close (worker->srv->logger, TRUE);
 
 	exit (EXIT_SUCCESS);
diff --git a/src/libcryptobox/cryptobox.c b/src/libcryptobox/cryptobox.c
index 0b4ebe614..e4549096f 100644
--- a/src/libcryptobox/cryptobox.c
+++ b/src/libcryptobox/cryptobox.c
@@ -310,6 +310,15 @@ rspamd_cryptobox_init (void)
 	return ctx;
 }
 
+void
+rspamd_cryptobox_deinit (struct rspamd_cryptobox_library_ctx *ctx)
+{
+	if (ctx) {
+		g_free (ctx->cpu_extensions);
+		g_free (ctx);
+	}
+}
+
 void
 rspamd_cryptobox_keypair (rspamd_pk_t pk, rspamd_sk_t sk,
 		enum rspamd_cryptobox_mode mode)
diff --git a/src/libcryptobox/cryptobox.h b/src/libcryptobox/cryptobox.h
index 3924d7fe3..61395d11f 100644
--- a/src/libcryptobox/cryptobox.h
+++ b/src/libcryptobox/cryptobox.h
@@ -84,6 +84,7 @@ struct rspamd_cryptobox_library_ctx {
  */
 struct rspamd_cryptobox_library_ctx *rspamd_cryptobox_init (void);
 
+void rspamd_cryptobox_deinit (struct rspamd_cryptobox_library_ctx *);
 /**
  * Generate new keypair
  * @param pk public key buffer
diff --git a/src/libserver/cfg_utils.c b/src/libserver/cfg_utils.c
index 25015c269..dd2578c91 100644
--- a/src/libserver/cfg_utils.c
+++ b/src/libserver/cfg_utils.c
@@ -124,10 +124,12 @@ struct rspamd_config *
 rspamd_config_new (enum rspamd_config_init_flags flags)
 {
 	struct rspamd_config *cfg;
+	rspamd_mempool_t *pool;
 
-	cfg = g_malloc0 (sizeof (*cfg));
+	pool = rspamd_mempool_new (8 * 1024 * 1024, "cfg");
+	cfg = rspamd_mempool_alloc0 (pool, sizeof (*cfg));
 	/* Allocate larger pool for cfg */
-	cfg->cfg_pool = rspamd_mempool_new (8 * 1024 * 1024, "cfg");
+	cfg->cfg_pool = pool;
 	cfg->dns_timeout = 1.0;
 	cfg->dns_retransmits = 5;
 	/* 16 sockets per DNS server */
@@ -311,7 +313,7 @@ rspamd_config_free (struct rspamd_config *cfg)
 
 	HASH_CLEAR (hh, cfg->actions);
 
-	rspamd_mempool_delete (cfg->cfg_pool);
+	rspamd_mempool_destructors_enforce (cfg->cfg_pool);
 
 	if (cfg->checksum) {
 		g_free (cfg->checksum);
@@ -324,7 +326,7 @@ rspamd_config_free (struct rspamd_config *cfg)
 		g_free (lp);
 	}
 
-	g_free (cfg);
+	rspamd_mempool_delete (cfg->cfg_pool);
 }
 
 const ucl_object_t *
diff --git a/src/libutil/http_context.c b/src/libutil/http_context.c
index f2ecaa037..9e2b09798 100644
--- a/src/libutil/http_context.c
+++ b/src/libutil/http_context.c
@@ -54,8 +54,7 @@ rspamd_http_keepalive_queue_cleanup (GQueue *conns)
 
 		cbd = (struct rspamd_http_keepalive_cbdata *)cur->data;
 		rspamd_http_connection_unref (cbd->conn);
-		/* Event is deleted here by deletion of the ev_base */
-		/* event_del (&cbd->ev); */
+		rspamd_ev_watcher_stop (cbd->ctx->event_loop, &cbd->ev);
 		g_free (cbd);
 
 		cur = cur->next;
@@ -173,8 +172,6 @@ rspamd_http_context_parse_proxy (struct rspamd_http_context *ctx,
 static void
 rspamd_http_context_init (struct rspamd_http_context *ctx)
 {
-
-
 	if (ctx->config.kp_cache_size_client > 0) {
 		ctx->client_kp_cache = rspamd_keypair_cache_new (ctx->config.kp_cache_size_client);
 	}
diff --git a/src/libutil/logger.c b/src/libutil/logger.c
index 8238886da..1a60b9e3f 100644
--- a/src/libutil/logger.c
+++ b/src/libutil/logger.c
@@ -357,9 +357,10 @@ rspamd_log_close_priv (rspamd_logger_t *rspamd_log, gboolean termination, uid_t
 		rspamd_log->opened = FALSE;
 	}
 
-	if (termination && rspamd_log->log_file) {
+	if (termination) {
 		g_free (rspamd_log->log_file);
 		rspamd_log->log_file = NULL;
+		g_free (rspamd_log);
 	}
 }
 
@@ -1445,6 +1446,15 @@ rspamd_logger_allocate_mod_bit (void)
 	}
 }
 
+RSPAMD_DESTRUCTOR (rspamd_debug_modules_dtor)
+{
+	if (log_modules) {
+		g_hash_table_unref (log_modules->modules);
+		g_free (log_modules->bitset);
+		g_free (log_modules);
+	}
+}
+
 guint
 rspamd_logger_add_debug_module (const gchar *mname)
 {
@@ -1455,9 +1465,13 @@ rspamd_logger_add_debug_module (const gchar *mname)
 	}
 
 	if (log_modules == NULL) {
+		/*
+		 * This is usually called from constructors, so we call init check
+		 * each time to avoid dependency issues between ctors calls
+		 */
 		log_modules = g_malloc0 (sizeof (*log_modules));
-		log_modules->modules = g_hash_table_new (rspamd_strcase_hash,
-				rspamd_strcase_equal);
+		log_modules->modules = g_hash_table_new_full (rspamd_strcase_hash,
+				rspamd_strcase_equal, g_free, g_free);
 		log_modules->bitset_allocated = 16;
 		log_modules->bitset_len = 0;
 		log_modules->bitset = g_malloc0 (log_modules->bitset_allocated);
diff --git a/src/libutil/regexp.c b/src/libutil/regexp.c
index 6f449539f..4ce9c4218 100644
--- a/src/libutil/regexp.c
+++ b/src/libutil/regexp.c
@@ -1055,6 +1055,7 @@ rspamd_regexp_cache_destroy (struct rspamd_regexp_cache *cache)
 		}
 #endif
 #endif
+		g_free (cache);
 	}
 }
 
diff --git a/src/libutil/util.c b/src/libutil/util.c
index 7877582c2..f528a321e 100644
--- a/src/libutil/util.c
+++ b/src/libutil/util.c
@@ -2573,6 +2573,8 @@ rspamd_deinit_libs (struct rspamd_external_libs_ctx *ctx)
 			ZSTD_freeDStream (ctx->in_zstream);
 		}
 
+		rspamd_cryptobox_deinit (ctx->crypto_ctx);
+
 		g_free (ctx);
 	}
 }
diff --git a/src/rspamadm/configtest.c b/src/rspamadm/configtest.c
index 6c07959db..1ebabe48b 100644
--- a/src/rspamadm/configtest.c
+++ b/src/rspamadm/configtest.c
@@ -128,12 +128,16 @@ rspamadm_configtest (gint argc, gchar **argv, const struct rspamadm_command *cmd
 	g_option_context_free (context);
 
 	if (config == NULL) {
+		static gchar fbuf[PATH_MAX];
+
 		if ((confdir = g_hash_table_lookup (ucl_vars, "CONFDIR")) == NULL) {
 			confdir = RSPAMD_CONFDIR;
 		}
 
-		config = g_strdup_printf ("%s%c%s", confdir, G_DIR_SEPARATOR,
+		rspamd_snprintf (fbuf, sizeof (fbuf), "%s%c%s",
+				confdir, G_DIR_SEPARATOR,
 				"rspamd.conf");
+		config = fbuf;
 	}
 
 	pworker = &workers[0];
diff --git a/src/rspamadm/rspamadm.c b/src/rspamadm/rspamadm.c
index 3a2bb4c53..2487207ef 100644
--- a/src/rspamadm/rspamadm.c
+++ b/src/rspamadm/rspamadm.c
@@ -619,8 +619,11 @@ end:
 	rspamd_http_context_free (rspamd_main->http_ctx);
 	rspamd_log_close (rspamd_main->logger, TRUE);
 	rspamd_url_deinit ();
-	g_free (rspamd_main);
 	g_ptr_array_free (all_commands, TRUE);
+	ev_loop_destroy (rspamd_main->event_loop);
+	g_hash_table_unref (ucl_vars);
+	rspamd_mempool_delete (rspamd_main->server_pool);
+	g_free (rspamd_main);
 
 	return retcode;
 }
diff --git a/src/rspamd_proxy.c b/src/rspamd_proxy.c
index 6b1eec237..be3395273 100644
--- a/src/rspamd_proxy.c
+++ b/src/rspamd_proxy.c
@@ -2230,6 +2230,9 @@ start_rspamd_proxy (struct rspamd_worker *worker)
 
 	ctx->http_ctx = rspamd_http_context_create (ctx->cfg, ctx->event_loop,
 			ctx->cfg->ups_ctx);
+	rspamd_mempool_add_destructor (ctx->cfg->cfg_pool,
+			(rspamd_mempool_destruct_t)rspamd_http_context_free,
+			ctx->http_ctx);
 
 	if (ctx->has_self_scan) {
 		/* Additional initialisation needed */
@@ -2263,9 +2266,7 @@ start_rspamd_proxy (struct rspamd_worker *worker)
 		rspamd_stat_close ();
 	}
 
-	struct rspamd_http_context *http_ctx = ctx->http_ctx;
 	REF_RELEASE (ctx->cfg);
-	rspamd_http_context_free (http_ctx);
 	rspamd_log_close (worker->srv->logger, TRUE);
 
 	exit (EXIT_SUCCESS);
diff --git a/src/worker.c b/src/worker.c
index 59f7f6e9e..0ed298181 100644
--- a/src/worker.c
+++ b/src/worker.c
@@ -668,6 +668,9 @@ start_worker (struct rspamd_worker *worker)
 
 	ctx->http_ctx = rspamd_http_context_create (ctx->cfg, ctx->event_loop,
 			ctx->cfg->ups_ctx);
+	rspamd_mempool_add_destructor (ctx->cfg->cfg_pool,
+			(rspamd_mempool_destruct_t)rspamd_http_context_free,
+			ctx->http_ctx);
 	rspamd_worker_init_scanner (worker, ctx->event_loop, ctx->resolver,
 			&ctx->lang_det);
 	rspamd_lua_run_postloads (ctx->cfg->lua_state, ctx->cfg, ctx->event_loop,
@@ -677,9 +680,7 @@ start_worker (struct rspamd_worker *worker)
 	rspamd_worker_block_signals ();
 
 	rspamd_stat_close ();
-	struct rspamd_http_context *http_ctx = ctx->http_ctx;
 	REF_RELEASE (ctx->cfg);
-	rspamd_http_context_free (http_ctx);
 	rspamd_log_close (worker->srv->logger, TRUE);
 
 	exit (EXIT_SUCCESS);


More information about the Commits mailing list