commit 68cf524: [Minor] Fix various static leaks

Vsevolod Stakhov vsevolod at highsecure.ru
Fri Sep 27 12:35:06 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-09-27 12:46:37 +0100
URL: https://github.com/rspamd/rspamd/commit/68cf5243462e0e9b743e420ab57a0be11055fc78

[Minor] Fix various static leaks

---
 src/rspamadm/commands.c      |  5 +++--
 src/rspamadm/configdump.c    |  4 +++-
 src/rspamadm/confighelp.c    |  2 ++
 src/rspamadm/configtest.c    | 11 ++++++-----
 src/rspamadm/control.c       |  3 +++
 src/rspamadm/dkim_keygen.c   |  2 ++
 src/rspamadm/fuzzy_convert.c |  3 +++
 src/rspamadm/lua_repl.c      |  3 +++
 src/rspamadm/pw.c            |  3 +++
 src/rspamadm/rspamadm.c      | 39 +++++++++++++++++++++++++++++++--------
 src/rspamadm/rspamadm.h      |  5 +++--
 src/rspamadm/signtool.c      |  3 +++
 src/rspamadm/stat_convert.c  |  3 +++
 13 files changed, 68 insertions(+), 18 deletions(-)

diff --git a/src/rspamadm/commands.c b/src/rspamadm/commands.c
index cf3143136..f379ff802 100644
--- a/src/rspamadm/commands.c
+++ b/src/rspamadm/commands.c
@@ -266,14 +266,15 @@ rspamadm_fill_lua_commands (lua_State *L, GPtrArray *dest)
 			lua_pushvalue (L, -1);
 			/* Reference table itself */
 			lua_cmd->command_data = GINT_TO_POINTER (luaL_ref (L, LUA_REGISTRYINDEX));
-			lua_cmd->flags |= RSPAMADM_FLAG_LUA;
+			lua_cmd->flags |= RSPAMADM_FLAG_LUA|RSPAMADM_FLAG_DYNAMIC;
 			lua_cmd->run = rspamadm_lua_command_run;
 			lua_cmd->help = rspamadm_lua_command_help;
 
-
 			g_ptr_array_add (dest, lua_cmd);
 		}
 
 		lua_settop (L, 0);
 	}
+
+	g_ptr_array_free (lua_paths, TRUE);
 }
diff --git a/src/rspamadm/configdump.c b/src/rspamadm/configdump.c
index 8f38cdf61..32e8ab3f1 100644
--- a/src/rspamadm/configdump.c
+++ b/src/rspamadm/configdump.c
@@ -263,9 +263,12 @@ rspamadm_configdump (gint argc, gchar **argv, const struct rspamadm_command *cmd
 	if (!g_option_context_parse (context, &argc, &argv, &error)) {
 		fprintf (stderr, "option parsing failed: %s\n", error->message);
 		g_error_free (error);
+		g_option_context_free (context);
 		exit (1);
 	}
 
+	g_option_context_free (context);
+
 	if (config == NULL) {
 		if ((confdir = g_hash_table_lookup (ucl_vars, "CONFDIR")) == NULL) {
 			confdir = RSPAMD_CONFDIR;
@@ -282,7 +285,6 @@ rspamadm_configdump (gint argc, gchar **argv, const struct rspamadm_command *cmd
 		pworker++;
 	}
 
-	cfg->cache = rspamd_symcache_new (cfg);
 	cfg->compiled_modules = modules;
 	cfg->compiled_workers = workers;
 	cfg->cfg_name = config;
diff --git a/src/rspamadm/confighelp.c b/src/rspamadm/confighelp.c
index a5b67a400..53f8dbbe9 100644
--- a/src/rspamadm/confighelp.c
+++ b/src/rspamadm/confighelp.c
@@ -216,9 +216,11 @@ rspamadm_confighelp (gint argc, gchar **argv, const struct rspamadm_command *cmd
 	if (!g_option_context_parse (context, &argc, &argv, &error)) {
 		rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message);
 		g_error_free (error);
+		g_option_context_free (context);
 		exit (1);
 	}
 
+	g_option_context_free (context);
 	pworker = &workers[0];
 	while (*pworker) {
 		/* Init string quarks */
diff --git a/src/rspamadm/configtest.c b/src/rspamadm/configtest.c
index 3b7a6b5b0..6c07959db 100644
--- a/src/rspamadm/configtest.c
+++ b/src/rspamadm/configtest.c
@@ -121,9 +121,12 @@ rspamadm_configtest (gint argc, gchar **argv, const struct rspamadm_command *cmd
 	if (!g_option_context_parse (context, &argc, &argv, &error)) {
 		fprintf (stderr, "option parsing failed: %s\n", error->message);
 		g_error_free (error);
+		g_option_context_free (context);
 		exit (1);
 	}
 
+	g_option_context_free (context);
+
 	if (config == NULL) {
 		if ((confdir = g_hash_table_lookup (ucl_vars, "CONFDIR")) == NULL) {
 			confdir = RSPAMD_CONFDIR;
@@ -139,7 +142,7 @@ rspamadm_configtest (gint argc, gchar **argv, const struct rspamadm_command *cmd
 		(void) g_quark_from_static_string ((*pworker)->name);
 		pworker++;
 	}
-	cfg->cache = rspamd_symcache_new (cfg);
+
 	cfg->compiled_modules = modules;
 	cfg->compiled_workers = workers;
 	cfg->cfg_name = config;
@@ -160,8 +163,8 @@ rspamadm_configtest (gint argc, gchar **argv, const struct rspamadm_command *cmd
 			ret = rspamd_config_post_load (cfg, RSPAMD_CONFIG_INIT_SYMCACHE);
 		}
 
-		if (ret && !rspamd_symcache_validate (rspamd_main->cfg->cache,
-				rspamd_main->cfg,
+		if (ret && !rspamd_symcache_validate (cfg->cache,
+				cfg,
 				FALSE)) {
 			ret = FALSE;
 		}
@@ -185,6 +188,4 @@ rspamadm_configtest (gint argc, gchar **argv, const struct rspamadm_command *cmd
 	if (!ret) {
 		exit (EXIT_FAILURE);
 	}
-
-	exit (EXIT_SUCCESS);
 }
diff --git a/src/rspamadm/control.c b/src/rspamadm/control.c
index 2f73d09ca..e605ecb0a 100644
--- a/src/rspamadm/control.c
+++ b/src/rspamadm/control.c
@@ -185,9 +185,12 @@ rspamadm_control (gint argc, gchar **argv, const struct rspamadm_command *_cmd)
 	if (!g_option_context_parse (context, &argc, &argv, &error)) {
 		rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message);
 		g_error_free (error);
+		g_option_context_free (context);
 		exit (1);
 	}
 
+	g_option_context_free (context);
+
 	if (argc <= 1) {
 		rspamd_fprintf (stderr, "command required\n");
 		exit (1);
diff --git a/src/rspamadm/dkim_keygen.c b/src/rspamadm/dkim_keygen.c
index 62b47f557..64e71f36e 100644
--- a/src/rspamadm/dkim_keygen.c
+++ b/src/rspamadm/dkim_keygen.c
@@ -356,8 +356,10 @@ rspamadm_dkim_keygen (gint argc, gchar **argv, const struct rspamadm_command *cm
 	if (!g_option_context_parse (context, &argc, &argv, &error)) {
 		fprintf (stderr, "option parsing failed: %s\n", error->message);
 		g_error_free (error);
+		g_option_context_free (context);
 		exit (1);
 	}
 
+	g_option_context_free (context);
 	rspamadm_dkim_generate_keypair (domain, selector, privkey_file, NULL, bits);
 }
diff --git a/src/rspamadm/fuzzy_convert.c b/src/rspamadm/fuzzy_convert.c
index c59200bc7..11386de65 100644
--- a/src/rspamadm/fuzzy_convert.c
+++ b/src/rspamadm/fuzzy_convert.c
@@ -93,9 +93,12 @@ rspamadm_fuzzyconvert (gint argc, gchar **argv, const struct rspamadm_command *c
 	if (!g_option_context_parse (context, &argc, &argv, &error)) {
 		rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message);
 		g_error_free (error);
+		g_option_context_free (context);
 		exit (1);
 	}
 
+	g_option_context_free (context);
+
 	if (!source_db) {
 		rspamd_fprintf (stderr, "source db is missing\n");
 		exit (1);
diff --git a/src/rspamadm/lua_repl.c b/src/rspamadm/lua_repl.c
index 54a2ed01e..17061929a 100644
--- a/src/rspamadm/lua_repl.c
+++ b/src/rspamadm/lua_repl.c
@@ -869,9 +869,12 @@ rspamadm_lua (gint argc, gchar **argv, const struct rspamadm_command *cmd)
 	if (!g_option_context_parse (context, &argc, &argv, &error)) {
 		fprintf (stderr, "option parsing failed: %s\n", error->message);
 		g_error_free (error);
+		g_option_context_free (context);
 		exit (1);
 	}
 
+	g_option_context_free (context);
+
 	if (batch == -1) {
 		if (isatty (STDIN_FILENO)) {
 			batch = 0;
diff --git a/src/rspamadm/pw.c b/src/rspamadm/pw.c
index b00aac223..0fea5c359 100644
--- a/src/rspamadm/pw.c
+++ b/src/rspamadm/pw.c
@@ -372,9 +372,12 @@ rspamadm_pw (gint argc, gchar **argv, const struct rspamadm_command *cmd)
 	if (!g_option_context_parse (context, &argc, &argv, &error)) {
 		fprintf (stderr, "option parsing failed: %s\n", error->message);
 		g_error_free (error);
+		g_option_context_free (context);
 		exit (1);
 	}
 
+	g_option_context_free (context);
+
 	if (list) {
 		rspamadm_alg_list ();
 		exit (EXIT_SUCCESS);
diff --git a/src/rspamadm/rspamadm.c b/src/rspamadm/rspamadm.c
index 565601956..6108a179b 100644
--- a/src/rspamadm/rspamadm.c
+++ b/src/rspamadm/rspamadm.c
@@ -352,6 +352,21 @@ rspamadm_add_lua_globals (struct rspamd_dns_resolver *resolver)
 	lua_setglobal (L, "rspamadm_dns_resolver");
 }
 
+static void
+rspamadm_cmd_dtor (gpointer p)
+{
+	struct rspamadm_command *cmd = (struct rspamadm_command *)p;
+
+	if (cmd->flags & RSPAMADM_FLAG_DYNAMIC) {
+		if (cmd->aliases) {
+			g_ptr_array_free (cmd->aliases, TRUE);
+		}
+
+		g_free ((gpointer)cmd->name);
+		g_free (cmd);
+	}
+}
+
 gint
 main (gint argc, gchar **argv, gchar **env)
 {
@@ -364,10 +379,12 @@ main (gint argc, gchar **argv, gchar **env)
 	const gchar *cmd_name;
 	const struct rspamadm_command *cmd;
 	struct rspamd_dns_resolver *resolver;
-	GPtrArray *all_commands = g_ptr_array_new (); /* Discovered during check */
+	GPtrArray *all_commands = g_ptr_array_new_full (32,
+			rspamadm_cmd_dtor); /* Discovered during check */
 	gint i, nargc, targc;
 	worker_t **pworker;
 	gboolean lua_file = FALSE;
+	gint retcode = 0;
 
 	ucl_vars = g_hash_table_new_full (rspamd_strcase_hash,
 		rspamd_strcase_equal, g_free, g_free);
@@ -417,9 +434,12 @@ main (gint argc, gchar **argv, gchar **env)
 	if (!g_option_context_parse (context, &targc, &targv, &error)) {
 		fprintf (stderr, "option parsing failed: %s\n", error->message);
 		g_error_free (error);
+		g_option_context_free (context);
 		exit (1);
 	}
 
+	g_option_context_free (context);
+
 	/* Setup logger */
 	if (verbose) {
 		cfg->log_level = G_LOG_LEVEL_DEBUG;
@@ -469,7 +489,7 @@ main (gint argc, gchar **argv, gchar **env)
 		rspamd_fprintf (stderr, "Cannot load lua environment: %e", error);
 		g_error_free (error);
 
-		exit (EXIT_FAILURE);
+		goto end;
 	}
 
 	rspamd_lua_set_globals (cfg, L);
@@ -500,15 +520,15 @@ main (gint argc, gchar **argv, gchar **env)
 
 	if (show_version) {
 		rspamadm_version ();
-		exit (EXIT_SUCCESS);
+		goto end;
 	}
 	if (show_help) {
 		rspamadm_usage (context);
-		exit (EXIT_SUCCESS);
+		goto end;
 	}
 	if (list_commands) {
 		rspamadm_commands (all_commands);
-		exit (EXIT_SUCCESS);
+		goto end;
 	}
 
 	cmd_name = argv[nargc];
@@ -547,7 +567,8 @@ main (gint argc, gchar **argv, gchar **env)
 			}
 		}
 
-		exit (EXIT_FAILURE);
+		retcode = EXIT_FAILURE;
+		goto end;
 	}
 
 	if (nargc < argc) {
@@ -592,12 +613,14 @@ main (gint argc, gchar **argv, gchar **env)
 
 	ev_break (rspamd_main->event_loop, EVBREAK_ALL);
 
-
+end:
+	rspamd_dns_resolver_deinit (resolver);
 	REF_RELEASE (rspamd_main->cfg);
 	rspamd_log_close (rspamd_main->logger, TRUE);
+	rspamd_url_deinit ();
 	g_free (rspamd_main);
 	g_ptr_array_free (all_commands, TRUE);
 
-	return 0;
+	return retcode;
 }
 
diff --git a/src/rspamadm/rspamadm.h b/src/rspamadm/rspamadm.h
index e1a638df7..de499112a 100644
--- a/src/rspamadm/rspamadm.h
+++ b/src/rspamadm/rspamadm.h
@@ -42,8 +42,9 @@ typedef void (*rspamadm_run_func) (gint argc, gchar **argv,
 
 typedef void (*rspamadm_lua_exports_func) (gpointer lua_state);
 
-#define RSPAMADM_FLAG_NOHELP (1 << 0)
-#define RSPAMADM_FLAG_LUA (1 << 1)
+#define RSPAMADM_FLAG_NOHELP (1u << 0u)
+#define RSPAMADM_FLAG_LUA (1u << 1u)
+#define RSPAMADM_FLAG_DYNAMIC (1u << 2u)
 
 struct rspamadm_command {
 	const gchar *name;
diff --git a/src/rspamadm/signtool.c b/src/rspamadm/signtool.c
index eae55b8bd..d92071bfa 100644
--- a/src/rspamadm/signtool.c
+++ b/src/rspamadm/signtool.c
@@ -489,9 +489,12 @@ rspamadm_signtool (gint argc, gchar **argv, const struct rspamadm_command *cmd)
 	if (!g_option_context_parse (context, &argc, &argv, &error)) {
 		rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message);
 		g_error_free (error);
+		g_option_context_free (context);
 		exit (1);
 	}
 
+	g_option_context_free (context);
+
 	if (openssl) {
 		mode = RSPAMD_CRYPTOBOX_MODE_NIST;
 	}
diff --git a/src/rspamadm/stat_convert.c b/src/rspamadm/stat_convert.c
index e3a3e179b..cea366f76 100644
--- a/src/rspamadm/stat_convert.c
+++ b/src/rspamadm/stat_convert.c
@@ -128,9 +128,12 @@ rspamadm_statconvert (gint argc, gchar **argv, const struct rspamadm_command *cm
 	if (!g_option_context_parse (context, &argc, &argv, &error)) {
 		rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message);
 		g_error_free (error);
+		g_option_context_free (context);
 		exit (1);
 	}
 
+	g_option_context_free (context);
+
 	if (config_file) {
 		/* Load config file, assuming that it has all information required */
 		struct ucl_parser *parser;


More information about the Commits mailing list