commit 78a0cb7: [Minor] Url: Fix parsing of numeric urls with a port

Vsevolod Stakhov vsevolod at highsecure.ru
Mon Apr 12 11:00:04 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-04-12 11:58:28 +0100
URL: https://github.com/rspamd/rspamd/commit/78a0cb71867a61124c09e60a843e1339d303e3a4 (HEAD -> master)

[Minor] Url: Fix parsing of numeric urls with a port

---
 src/libserver/url.c | 11 +++++++++++
 src/lua/lua_url.c   | 20 +++++++++++---------
 2 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/src/libserver/url.c b/src/libserver/url.c
index 8183213b6..1548c4535 100644
--- a/src/libserver/url.c
+++ b/src/libserver/url.c
@@ -1715,11 +1715,16 @@ rspamd_url_regen_from_inet_addr (struct rspamd_url *uri, const void *addr, int a
 		slen += INET6_ADDRSTRLEN;
 	}
 
+	if (uri->flags & RSPAMD_URL_FLAG_HAS_PORT) {
+		slen += sizeof ("65535") - 1;
+	}
+
 	/* Allocate new string to build it from IP */
 	strbuf = rspamd_mempool_alloc (pool, slen + 1);
 	r += rspamd_snprintf (strbuf + r, slen - r, "%*s",
 			(gint)(uri->hostshift),
 			uri->string);
+
 	uri->hostshift = r;
 	uri->tldshift = r;
 	start_offset = strbuf + r;
@@ -1730,6 +1735,12 @@ rspamd_url_regen_from_inet_addr (struct rspamd_url *uri, const void *addr, int a
 	uri->flags |= RSPAMD_URL_FLAG_NUMERIC;
 
 	/* Reconstruct URL */
+	if (uri->flags & RSPAMD_URL_FLAG_HAS_PORT) {
+		p = strbuf + r;
+		start_offset = p + 1;
+		r += rspamd_snprintf (strbuf + r, slen - r, ":%ud",
+				(unsigned int)uri->port);
+	}
 	if (uri->datalen > 0) {
 		p = strbuf + r;
 		start_offset = p + 1;
diff --git a/src/lua/lua_url.c b/src/lua/lua_url.c
index 37bccd1b2..ad938c052 100644
--- a/src/lua/lua_url.c
+++ b/src/lua/lua_url.c
@@ -719,6 +719,16 @@ lua_url_to_table (lua_State *L)
 	return 1;
 }
 
+static rspamd_mempool_t *static_lua_url_pool;
+
+RSPAMD_CONSTRUCTOR(rspamd_urls_static_pool_ctor) {
+	static_lua_url_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (),
+			"static_lua_url", 0);
+}
+
+RSPAMD_DESTRUCTOR(rspamd_urls_static_pool_dtor) {
+	rspamd_mempool_delete (static_lua_url_pool);
+}
 
 /***
  * @function url.create([mempool,] str, [{flags_table}])
@@ -742,15 +752,11 @@ lua_url_create (lua_State *L)
 	}
 	else {
 		own_pool = TRUE;
-		pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), "url", 0);
+		pool = static_lua_url_pool;
 		text = luaL_checklstring (L, 1, &length);
 	}
 
 	if (pool == NULL || text == NULL) {
-		if (own_pool && pool) {
-			rspamd_mempool_delete (pool);
-		}
-
 		return luaL_error (L, "invalid arguments");
 	}
 	else {
@@ -783,10 +789,6 @@ lua_url_create (lua_State *L)
 		}
 	}
 
-	if (own_pool && pool) {
-		rspamd_mempool_delete (pool);
-	}
-
 	return 1;
 }
 


More information about the Commits mailing list