From 8ca6424889cf9d993d600e0a64b480e5b2b178d8 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 15 Jun 2018 15:03:19 +0100 Subject: [PATCH] [Minor] Fix leak in proctitle init --- src/libutil/util.c | 59 ++++++++++++++++++++++++++++------------------ src/libutil/util.h | 2 +- src/rspamd.c | 2 +- 3 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/libutil/util.c b/src/libutil/util.c index 510b16045..3193f8e8d 100644 --- a/src/libutil/util.c +++ b/src/libutil/util.c @@ -852,13 +852,29 @@ setproctitle (const gchar *fmt, ...) #endif } +#if !(defined(DARWIN) || defined(SOLARIS) || defined(__APPLE__)) +static void +rspamd_title_dtor (gpointer d) +{ + gchar **env = (gchar **)d; + guint i; + + for (i = 0; env[i] != NULL; i++) { + g_free (env[i]); + } + + g_free (env); +} +#endif + /* It has to be _init function, because __attribute__((constructor)) functions gets called without arguments. */ gint -init_title (gint argc, gchar *argv[], gchar *envp[]) +init_title (struct rspamd_main *rspamd_main, + gint argc, gchar *argv[], gchar *envp[]) { #if defined(DARWIN) || defined(SOLARIS) || defined(__APPLE__) /* XXX: try to handle these OSes too */ @@ -868,45 +884,46 @@ init_title (gint argc, gchar *argv[], gchar *envp[]) gint i; for (i = 0; i < argc; ++i) { - if (!begin_of_buffer) + if (!begin_of_buffer) { begin_of_buffer = argv[i]; - if (!end_of_buffer || end_of_buffer + 1 == argv[i]) + } + if (!end_of_buffer || end_of_buffer + 1 == argv[i]) { end_of_buffer = argv[i] + strlen (argv[i]); + } } for (i = 0; envp[i]; ++i) { - if (!begin_of_buffer) + if (!begin_of_buffer) { begin_of_buffer = envp[i]; - if (!end_of_buffer || end_of_buffer + 1 == envp[i]) + } + if (!end_of_buffer || end_of_buffer + 1 == envp[i]) { end_of_buffer = envp[i] + strlen (envp[i]); + } } - if (!end_of_buffer) + if (!end_of_buffer) { return 0; + } gchar **new_environ = g_malloc ((i + 1) * sizeof (envp[0])); - if (!new_environ) - return 0; - for (i = 0; envp[i]; ++i) { - if (!(new_environ[i] = g_strdup (envp[i]))) - goto cleanup_enomem; + new_environ[i] = g_strdup (envp[i]); } - new_environ[i] = 0; + + new_environ[i] = NULL; if (program_invocation_name) { title_progname_full = g_strdup (program_invocation_name); - if (!title_progname_full) - goto cleanup_enomem; - gchar *p = strrchr (title_progname_full, '/'); - if (p) + if (p) { title_progname = p + 1; - else + } + else { title_progname = title_progname_full; + } program_invocation_name = title_progname_full; program_invocation_short_name = title_progname; @@ -916,13 +933,9 @@ init_title (gint argc, gchar *argv[], gchar *envp[]) title_buffer = begin_of_buffer; title_buffer_size = end_of_buffer - begin_of_buffer; - return 0; + rspamd_mempool_add_destructor (rspamd_main->server_pool, + rspamd_title_dtor, new_environ); -cleanup_enomem: - for (--i; i >= 0; --i) { - g_free (new_environ[i]); - } - g_free (new_environ); return 0; #endif } diff --git a/src/libutil/util.h b/src/libutil/util.h index 96e85af30..5968094a9 100644 --- a/src/libutil/util.h +++ b/src/libutil/util.h @@ -116,7 +116,7 @@ void rspamd_pass_signal (GHashTable *, gint ); /* * Process title utility functions */ -gint init_title (gint argc, gchar *argv[], gchar *envp[]); +gint init_title (struct rspamd_main *, gint argc, gchar *argv[], gchar *envp[]); gint setproctitle (const gchar *fmt, ...); #endif diff --git a/src/rspamd.c b/src/rspamd.c index 34436bd10..307aa52ad 100644 --- a/src/rspamd.c +++ b/src/rspamd.c @@ -1188,7 +1188,7 @@ main (gint argc, gchar **argv, gchar **env) rspamd_main->start_mtx = rspamd_mempool_get_mutex (rspamd_main->server_pool); #ifndef HAVE_SETPROCTITLE - init_title (argc, argv, env); + init_title (rspamd_main, argc, argv, env); #endif rspamd_main->cfg->libs_ctx = rspamd_init_libs (); -- 2.39.5