commit 4041c2d: [Minor] Plug some leaks on error paths

Vsevolod Stakhov vsevolod at highsecure.ru
Sat Sep 18 12:00:05 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-09-18 12:29:57 +0100
URL: https://github.com/rspamd/rspamd/commit/4041c2d64024f27488461e03fcd587dfbb34c4b0

[Minor] Plug some leaks on error paths
Found by: coverity scan

---
 src/controller.c                 |  3 ++-
 src/libserver/cfg_rcl.c          | 19 ++++++++++++-------
 src/libserver/maps/map.c         |  2 ++
 src/libserver/maps/map_helpers.c |  1 +
 src/libstat/stat_process.c       |  3 +++
 src/lua/lua_http.c               |  6 ++++++
 src/lua/lua_rsa.c                |  2 ++
 src/lua/lua_text.c               |  6 +++++-
 src/lua/lua_url.c                |  2 ++
 src/rspamadm/lua_repl.c          |  3 ++-
 src/rspamd.c                     |  3 ++-
 src/rspamd_proxy.c               |  3 ++-
 src/worker.c                     |  4 +++-
 test/rspamd_http_test.c          |  3 ++-
 utils/rspamd_http_server.c       |  1 +
 15 files changed, 47 insertions(+), 14 deletions(-)

diff --git a/src/controller.c b/src/controller.c
index 0f9a762e1..a34bb6034 100644
--- a/src/controller.c
+++ b/src/controller.c
@@ -3563,7 +3563,7 @@ rspamd_controller_accept_socket (EV_P_ ev_io *w, int revents)
 	struct rspamd_worker *worker = (struct rspamd_worker *)w->data;
 	struct rspamd_controller_worker_ctx *ctx;
 	struct rspamd_controller_session *session;
-	rspamd_inet_addr_t *addr;
+	rspamd_inet_addr_t *addr = NULL;
 	gint nfd;
 
 	ctx = worker->ctx;
@@ -3576,6 +3576,7 @@ rspamd_controller_accept_socket (EV_P_ ev_io *w, int revents)
 	}
 	/* Check for EAGAIN */
 	if (nfd == 0) {
+		rspamd_inet_address_free (addr);
 		return;
 	}
 
diff --git a/src/libserver/cfg_rcl.c b/src/libserver/cfg_rcl.c
index 06b7edee2..47d487ee0 100644
--- a/src/libserver/cfg_rcl.c
+++ b/src/libserver/cfg_rcl.c
@@ -3712,22 +3712,25 @@ rspamd_config_parse_ucl (struct rspamd_config *cfg,
 	struct rspamd_cryptobox_keypair *decrypt_keypair = NULL;
 	gchar *data;
 
-	if (stat (filename, &st) == -1) {
-		g_set_error (err, cfg_rcl_error_quark (), errno,
-				"cannot stat %s: %s", filename, strerror (errno));
-		return FALSE;
-	}
 	if ((fd = open (filename, O_RDONLY)) == -1) {
 		g_set_error (err, cfg_rcl_error_quark (), errno,
 				"cannot open %s: %s", filename, strerror (errno));
 		return FALSE;
 
 	}
+	if (fstat (fd, &st) == -1) {
+		g_set_error (err, cfg_rcl_error_quark (), errno,
+				"cannot stat %s: %s", filename, strerror (errno));
+		close (fd);
+
+		return FALSE;
+	}
 	/* Now mmap this file to simplify reading process */
 	if ((data = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
 		g_set_error (err, cfg_rcl_error_quark (), errno,
 				"cannot mmap %s: %s", filename, strerror (errno));
 		close (fd);
+
 		return FALSE;
 	}
 
@@ -3736,8 +3739,7 @@ rspamd_config_parse_ucl (struct rspamd_config *cfg,
 	/* Try to load keyfile if available */
 	rspamd_snprintf (keypair_path, sizeof (keypair_path), "%s.key",
 			filename);
-	if (stat (keypair_path, &st) == -1 &&
-		(fd = open (keypair_path, O_RDONLY)) != -1) {
+	if ((fd = open (keypair_path, O_RDONLY)) != -1) {
 		struct ucl_parser *kp_parser;
 
 		kp_parser = ucl_parser_new (0);
@@ -3769,6 +3771,7 @@ rspamd_config_parse_ucl (struct rspamd_config *cfg,
 		}
 
 		ucl_parser_free (kp_parser);
+		close (fd);
 	}
 
 	parser = ucl_parser_new (UCL_PARSER_SAVE_COMMENTS);
@@ -4178,6 +4181,8 @@ rspamd_rcl_add_doc_by_path (struct rspamd_config *cfg,
 				cur = found;
 			}
 		}
+
+		g_strfreev (path_components);
 	}
 
 	return rspamd_rcl_add_doc_obj (ucl_object_ref (cur),
diff --git a/src/libserver/maps/map.c b/src/libserver/maps/map.c
index f324f9d70..eaf7958f8 100644
--- a/src/libserver/maps/map.c
+++ b/src/libserver/maps/map.c
@@ -596,6 +596,8 @@ read_map_file_chunks (struct rspamd_map *map, struct map_cb_data *cbdata,
 	if (lseek (fd, off, SEEK_SET) == -1) {
 		msg_err_map ("can't seek in map to pos %d for buffered reading %s: %s",
 				(gint)off, fname, strerror (errno));
+		close (fd);
+
 		return FALSE;
 	}
 
diff --git a/src/libserver/maps/map_helpers.c b/src/libserver/maps/map_helpers.c
index f98e4dafc..51bc9710c 100644
--- a/src/libserver/maps/map_helpers.c
+++ b/src/libserver/maps/map_helpers.c
@@ -1868,6 +1868,7 @@ rspamd_cdb_list_read (gchar *chunk,
 		cdb = g_malloc0 (sizeof (struct cdb));
 
 		if (cdb_init (cdb, fd) == -1) {
+			g_free (cdb);
 			msg_err_map ("cannot init cdb map from %s: %s", chunk, strerror (errno));
 
 			return NULL;
diff --git a/src/libstat/stat_process.c b/src/libstat/stat_process.c
index 13083986f..2c77942b6 100644
--- a/src/libstat/stat_process.c
+++ b/src/libstat/stat_process.c
@@ -1213,6 +1213,9 @@ rspamd_stat_statistics (struct rspamd_task *task,
 	if (target) {
 		*target = res;
 	}
+	else {
+		ucl_object_unref (res);
+	}
 
 	return RSPAMD_STAT_PROCESS_OK;
 }
diff --git a/src/lua/lua_http.c b/src/lua/lua_http.c
index d85664ef0..a92c1fe8f 100644
--- a/src/lua/lua_http.c
+++ b/src/lua/lua_http.c
@@ -978,6 +978,12 @@ lua_http_request (lua_State *L)
 	}
 
 	if (ev_base == NULL) {
+		g_free (auth);
+		rspamd_http_message_unref (msg);
+		if (body) {
+			rspamd_fstring_free (body);
+		}
+
 		return luaL_error (L,
 				"Bad params to rspamd_http:request(): ev_base isn't passed");
 	}
diff --git a/src/lua/lua_rsa.c b/src/lua/lua_rsa.c
index 4b2caa2bf..5e34bc2c6 100644
--- a/src/lua/lua_rsa.c
+++ b/src/lua/lua_rsa.c
@@ -664,6 +664,8 @@ lua_rsa_sign_memory (lua_State *L)
 				signature->str, (guint *)&signature->len, rsa);
 
 		if (ret != 1) {
+			rspamd_fstring_free (signature);
+
 			return luaL_error (L, "cannot sign: %s",
 					ERR_error_string (ERR_get_error (), NULL));
 		}
diff --git a/src/lua/lua_text.c b/src/lua/lua_text.c
index dde7e2c23..bec16c3b6 100644
--- a/src/lua/lua_text.c
+++ b/src/lua/lua_text.c
@@ -920,6 +920,10 @@ lua_text_split (lua_State *L)
 	struct rspamd_lua_regexp *re;
 	gboolean stringify = FALSE, own_re = FALSE;
 
+	if (t == NULL) {
+		return luaL_error (L, "invalid arguments");
+	}
+
 	if (lua_type (L, 2) == LUA_TUSERDATA) {
 		re = lua_check_regexp (L, 2);
 	}
@@ -947,7 +951,7 @@ lua_text_split (lua_State *L)
 		own_re = TRUE;
 	}
 
-	if (t && re) {
+	if (re) {
 		if (lua_isboolean (L, 3)) {
 			stringify = lua_toboolean (L, 3);
 		}
diff --git a/src/lua/lua_url.c b/src/lua/lua_url.c
index 3298c7bed..26dd1eb7c 100644
--- a/src/lua/lua_url.c
+++ b/src/lua/lua_url.c
@@ -1169,6 +1169,8 @@ lua_url_cbdata_fill (lua_State *L,
 			}
 			else {
 				msg_info ("bad url protocol: %s", *cvec);
+				g_strfreev (strvec);
+
 				return FALSE;
 			}
 
diff --git a/src/rspamadm/lua_repl.c b/src/rspamadm/lua_repl.c
index 3497a85b8..70ce06cd2 100644
--- a/src/rspamadm/lua_repl.c
+++ b/src/rspamadm/lua_repl.c
@@ -721,7 +721,7 @@ rspamadm_lua_accept_cb (EV_P_ ev_io *w, int revents)
 {
 	struct rspamadm_lua_repl_context *ctx =
 			(struct rspamadm_lua_repl_context *)w->data;
-	rspamd_inet_addr_t *addr;
+	rspamd_inet_addr_t *addr = NULL;
 	struct rspamadm_lua_repl_session *session;
 	gint nfd;
 
@@ -732,6 +732,7 @@ rspamadm_lua_accept_cb (EV_P_ ev_io *w, int revents)
 	}
 	/* Check for EAGAIN */
 	if (nfd == 0) {
+		rspamd_inet_address_free (addr);
 		return;
 	}
 
diff --git a/src/rspamd.c b/src/rspamd.c
index 987ac8c5c..fd602dd4c 100644
--- a/src/rspamd.c
+++ b/src/rspamd.c
@@ -1212,7 +1212,7 @@ static void
 rspamd_control_handler (EV_P_ ev_io *w, int revents)
 {
 	struct rspamd_main *rspamd_main = (struct rspamd_main *)w->data;
-	rspamd_inet_addr_t *addr;
+	rspamd_inet_addr_t *addr = NULL;
 	gint nfd;
 
 	if ((nfd =
@@ -1222,6 +1222,7 @@ rspamd_control_handler (EV_P_ ev_io *w, int revents)
 	}
 	/* Check for EAGAIN */
 	if (nfd == 0) {
+		rspamd_inet_address_free (addr);
 		return;
 	}
 
diff --git a/src/rspamd_proxy.c b/src/rspamd_proxy.c
index 267760c03..a7944eecf 100644
--- a/src/rspamd_proxy.c
+++ b/src/rspamd_proxy.c
@@ -2185,7 +2185,7 @@ proxy_accept_socket (EV_P_ ev_io *w, int revents)
 {
 	struct rspamd_worker *worker = (struct rspamd_worker *)w->data;
 	struct rspamd_proxy_ctx *ctx;
-	rspamd_inet_addr_t *addr;
+	rspamd_inet_addr_t *addr = NULL;
 	struct rspamd_proxy_session *session;
 	gint nfd;
 
@@ -2199,6 +2199,7 @@ proxy_accept_socket (EV_P_ ev_io *w, int revents)
 	}
 	/* Check for EAGAIN */
 	if (nfd == 0) {
+		rspamd_inet_address_free (addr);
 		return;
 	}
 
diff --git a/src/worker.c b/src/worker.c
index 5262f7cde..c2d00d9cd 100644
--- a/src/worker.c
+++ b/src/worker.c
@@ -342,7 +342,7 @@ accept_socket (EV_P_ ev_io *w, int revents)
 	struct rspamd_worker *worker = (struct rspamd_worker *) w->data;
 	struct rspamd_worker_ctx *ctx;
 	struct rspamd_worker_session *session;
-	rspamd_inet_addr_t *addr;
+	rspamd_inet_addr_t *addr = NULL;
 	gint nfd, http_opts = 0;
 
 	ctx = worker->ctx;
@@ -362,6 +362,8 @@ accept_socket (EV_P_ ev_io *w, int revents)
 	}
 	/* Check for EAGAIN */
 	if (nfd == 0) {
+		rspamd_inet_address_free (addr);
+
 		return;
 	}
 
diff --git a/test/rspamd_http_test.c b/test/rspamd_http_test.c
index c783ccf21..1b271a7cc 100644
--- a/test/rspamd_http_test.c
+++ b/test/rspamd_http_test.c
@@ -53,7 +53,7 @@ static void
 rspamd_server_accept (gint fd, short what, void *arg)
 {
 	struct rspamd_http_connection_router *rt = arg;
-	rspamd_inet_addr_t *addr;
+	rspamd_inet_addr_t *addr = NULL;
 	gint nfd;
 
 	if ((nfd =
@@ -63,6 +63,7 @@ rspamd_server_accept (gint fd, short what, void *arg)
 	}
 	/* Check for EAGAIN */
 	if (nfd == 0) {
+		rspamd_inet_address_free (addr);
 		return;
 	}
 
diff --git a/utils/rspamd_http_server.c b/utils/rspamd_http_server.c
index f4b8e3b5f..679ffa0b6 100644
--- a/utils/rspamd_http_server.c
+++ b/utils/rspamd_http_server.c
@@ -145,6 +145,7 @@ rspamd_server_accept (gint fd, short what, void *arg)
 		}
 		/* Check for EAGAIN */
 		if (nfd == 0) {
+			rspamd_inet_address_free (addr);
 			return;
 		}
 


More information about the Commits mailing list