commit 67f1dab: [Project] Fix init parts

Vsevolod Stakhov vsevolod at highsecure.ru
Fri Jul 12 16:42:10 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-07-11 16:10:07 +0100
URL: https://github.com/rspamd/rspamd/commit/67f1dab4bdfac3aeb827df6336acfe76f469811c

[Project] Fix init parts

---
 src/libmime/message.c      | 85 ++++++++++++++++++++++++++++++----------------
 src/libmime/message.h      |  4 +--
 src/libmime/mime_headers.c | 27 ++++++++++-----
 src/libmime/mime_headers.h | 12 +++++++
 src/libserver/task.c       | 34 -------------------
 5 files changed, 87 insertions(+), 75 deletions(-)

diff --git a/src/libmime/message.c b/src/libmime/message.c
index 500590e02..1ecd38629 100644
--- a/src/libmime/message.c
+++ b/src/libmime/message.c
@@ -36,6 +36,7 @@
 #include <unicode/uchar.h>
 #include "libserver/cfg_file_private.h"
 #include "lua/lua_common.h"
+#include "contrib/uthash/utlist.h"
 
 #define GTUBE_SYMBOL "GTUBE"
 
@@ -1061,12 +1062,9 @@ rspamd_message_from_data (struct rspamd_task *task, const guchar *start,
 	part->raw_data.len = len;
 	part->parsed_data.begin = start;
 	part->parsed_data.len = len;
-	part->id = task->parts->len;
-	part->raw_headers =  g_hash_table_new_full (rspamd_strcase_hash,
-			rspamd_strcase_equal, NULL, rspamd_ptr_array_free_hard);
-	part->headers_order = g_queue_new ();
-
-
+	part->id = MESSAGE_FIELD (task, parts)->len;
+	part->raw_headers = rspamd_message_headers_new ();
+	part->headers_order = NULL;
 
 	tok = rspamd_task_get_request_header (task, "Filename");
 
@@ -1080,21 +1078,57 @@ rspamd_message_from_data (struct rspamd_task *task, const guchar *start,
 	part->cd = rspamd_content_disposition_parse (cdbuf, strlen (cdbuf),
 			task->task_pool);
 
-	g_ptr_array_add (task->parts, part);
+	g_ptr_array_add (MESSAGE_FIELD (task, parts), part);
 	rspamd_mime_parser_calc_digest (part);
 
 	/* Generate message ID */
 	mid = rspamd_mime_message_id_generate ("localhost.localdomain");
 	rspamd_mempool_add_destructor (task->task_pool,
 			(rspamd_mempool_destruct_t) g_free, mid);
-	task->message_id = mid;
+	MESSAGE_FIELD (task, message_id) = mid;
 	task->queue_id = mid;
 }
 
 static void
 rspamd_message_dtor (struct rspamd_message *msg)
 {
+	guint i;
+	struct rspamd_mime_part *p;
+	struct rspamd_mime_text_part *tp;
+
 
+	PTR_ARRAY_FOREACH (msg->parts, i, p) {
+		if (p->raw_headers) {
+			rspamd_message_headers_destroy (p->raw_headers);
+		}
+
+		if (IS_CT_MULTIPART (p->ct)) {
+			if (p->specific.mp->children) {
+				g_ptr_array_free (p->specific.mp->children, TRUE);
+			}
+		}
+	}
+
+	PTR_ARRAY_FOREACH (msg->text_parts, i, tp) {
+		if (tp->utf_words) {
+			g_array_free (tp->utf_words, TRUE);
+		}
+		if (tp->normalized_hashes) {
+			g_array_free (tp->normalized_hashes, TRUE);
+		}
+		if (tp->languages) {
+			g_ptr_array_unref (tp->languages);
+		}
+	}
+
+	g_ptr_array_unref (msg->text_parts);
+	g_ptr_array_unref (msg->parts);
+
+	g_ptr_array_unref (msg->from_mime);
+	g_ptr_array_unref (msg->rcpt_mime);
+
+	g_hash_table_unref (msg->urls);
+	g_hash_table_unref (msg->emails);
 }
 
 struct rspamd_message*
@@ -1102,9 +1136,15 @@ rspamd_message_new (struct rspamd_task *task)
 {
 	struct rspamd_message *msg;
 
-	msg = rspamd_mempool_alloc0 (sizeof (*msg));
+	msg = rspamd_mempool_alloc0 (task->task_pool, sizeof (*msg));
 
+	msg->raw_headers = rspamd_message_headers_new ();
+	msg->emails = g_hash_table_new (rspamd_email_hash, rspamd_emails_cmp);
+	msg->urls = g_hash_table_new (rspamd_url_hash, rspamd_urls_cmp);
+	msg->parts = g_ptr_array_sized_new (4);
+	msg->text_parts = g_ptr_array_sized_new (2);
 
+	return msg;
 }
 
 gboolean
@@ -1163,6 +1203,7 @@ rspamd_message_parse (struct rspamd_task *task)
 	task->msg.begin = p;
 	task->msg.len = len;
 	rspamd_cryptobox_hash_init (&st, NULL, 0);
+	task->message = rspamd_message_new (task);s
 
 	if (task->flags & RSPAMD_TASK_FLAG_MIME) {
 		enum rspamd_mime_parse_error ret;
@@ -1206,8 +1247,8 @@ rspamd_message_parse (struct rspamd_task *task)
 	}
 
 
-	if (task->message_id == NULL) {
-		task->message_id = "undef";
+	if (MESSAGE_FIELD (task, message_id) == NULL) {
+		MESSAGE_FIELD (task, message_id) = "undef";
 	}
 
 	debug_task ("found %ud parts in message", task->parts->len);
@@ -1215,11 +1256,11 @@ rspamd_message_parse (struct rspamd_task *task)
 		task->queue_id = "undef";
 	}
 
-	if (task->received->len > 0) {
+	if (MESSAGE_FIELD (task, received)) {
 		gboolean need_recv_correction = FALSE;
 		rspamd_inet_addr_t *raddr;
 
-		recv = g_ptr_array_index (task->received, 0);
+		recv = MESSAGE_FIELD (task, received);
 		/*
 		 * For the first header we must ensure that
 		 * received is consistent with the IP that we obtain through
@@ -1273,23 +1314,7 @@ rspamd_message_parse (struct rspamd_task *task)
 				trecv->from_hostname = trecv->real_hostname;
 			}
 
-#ifdef GLIB_VERSION_2_40
-			g_ptr_array_insert (task->received, 0, trecv);
-#else
-			/*
-			 * Unfortunately, before glib 2.40 we cannot insert element into a
-			 * ptr array
-			 */
-			GPtrArray *nar = g_ptr_array_sized_new (task->received->len + 1);
-
-			g_ptr_array_add (nar, trecv);
-			PTR_ARRAY_FOREACH (task->received, i, recv) {
-				g_ptr_array_add (nar, recv);
-			}
-			rspamd_mempool_add_destructor (task->task_pool,
-						rspamd_ptr_array_free_hard, nar);
-			task->received = nar;
-#endif
+			DL_PREPEND (MESSAGE_FIELD (task, received), trecv);
 		}
 	}
 
diff --git a/src/libmime/message.h b/src/libmime/message.h
index 9cf67ba82..25d80bee9 100644
--- a/src/libmime/message.h
+++ b/src/libmime/message.h
@@ -146,8 +146,8 @@ struct rspamd_message {
 	struct rspamd_received_header *received;	/**< list of received headers						*/
 	GHashTable *urls;							/**< list of parsed urls							*/
 	GHashTable *emails;							/**< list of parsed emails							*/
-	khash_t(rspamd_mime_headers_htb) *raw_headers;	/**< list of raw headers							*/
-	struct rspamd_mime_header *headers_order;	/**< inversed order of raw headers							*/
+	khash_t(rspamd_mime_headers_htb) *raw_headers;	/**< list of raw headers						*/
+	struct rspamd_mime_header *headers_order;	/**< order of raw headers							*/
 	GPtrArray *rcpt_mime;
 	GPtrArray *from_mime;
 	enum rspamd_newlines_type nlines_type; 		/**< type of newlines (detected on most of headers 	*/
diff --git a/src/libmime/mime_headers.c b/src/libmime/mime_headers.c
index 7ffa5453c..bc8ace679 100644
--- a/src/libmime/mime_headers.c
+++ b/src/libmime/mime_headers.c
@@ -390,7 +390,7 @@ rspamd_mime_headers_process (struct rspamd_task *task,
 			/* We also validate utf8 and replace all non-valid utf8 chars */
 			rspamd_mime_charset_utf_enforce (nh->decoded, strlen (nh->decoded));
 			nh->order = norder ++;
-			rspamd_mime_header_add (task, target, order, nh, check_newlines);
+			rspamd_mime_header_add (task, target, order_ptr, nh, check_newlines);
 			nh = NULL;
 			state = 0;
 			break;
@@ -400,7 +400,7 @@ rspamd_mime_headers_process (struct rspamd_task *task,
 			nh->decoded = "";
 			nh->raw_len = p - nh->raw_value;
 			nh->order = norder ++;
-			rspamd_mime_header_add (task, target, order, nh, check_newlines);
+			rspamd_mime_header_add (task, target, order_ptr, nh, check_newlines);
 			nh = NULL;
 			state = 0;
 			break;
@@ -463,10 +463,12 @@ rspamd_mime_headers_process (struct rspamd_task *task,
 		}
 	}
 
+	/* Since we have prepended headers, we need to reverse the list to get the actual order */
+	LL_REVERSE (*order_ptr);
+
 	if (check_newlines) {
 		guint max_cnt = 0;
 		gint sel = 0;
-		GList *cur;
 		rspamd_cryptobox_hash_state_t hs;
 		guchar hout[rspamd_cryptobox_HASHBYTES], *hexout;
 
@@ -479,17 +481,12 @@ rspamd_mime_headers_process (struct rspamd_task *task,
 
 		MESSAGE_FIELD (task, nlines_type) = sel;
 
-		cur = order->head;
 		rspamd_cryptobox_hash_init (&hs, NULL, 0);
 
-		while (cur) {
-			nh = cur->data;
-
+		LL_FOREACH (*order_ptr, nh) {
 			if (nh->name && nh->flags != RSPAMD_HEADER_RECEIVED) {
 				rspamd_cryptobox_hash_update (&hs, nh->name, strlen (nh->name));
 			}
-
-			cur = g_list_next (cur);
 		}
 
 		rspamd_cryptobox_hash_final (&hs, hout);
@@ -1620,4 +1617,16 @@ rspamd_message_get_header_array (struct rspamd_task *task,
 {
 	return rspamd_message_get_header_from_hash (MESSAGE_FIELD (task, raw_headers),
 			field);
+}
+
+void
+rspamd_message_headers_destroy (khash_t(rspamd_mime_headers_htb) *htb)
+{
+	kh_destroy (rspamd_mime_headers_htb, htb);
+}
+
+khash_t(rspamd_mime_headers_htb) *
+rspamd_message_headers_new (void)
+{
+	return kh_init (rspamd_mime_headers_htb);
 }
\ No newline at end of file
diff --git a/src/libmime/mime_headers.h b/src/libmime/mime_headers.h
index 7c17b8ddd..81948d11b 100644
--- a/src/libmime/mime_headers.h
+++ b/src/libmime/mime_headers.h
@@ -156,6 +156,18 @@ struct rspamd_mime_header *
 rspamd_message_get_header_from_hash (khash_t(rspamd_mime_headers_htb) *htb,
 									 const gchar *field);
 
+/**
+ * Cleans up hash table of the headers
+ * @param htb
+ */
+void rspamd_message_headers_destroy (khash_t(rspamd_mime_headers_htb) *htb);
+
+/**
+ * Init headers hash
+ * @return
+ */
+khash_t(rspamd_mime_headers_htb)* rspamd_message_headers_new (void);
+
 #ifdef  __cplusplus
 }
 #endif
diff --git a/src/libserver/task.c b/src/libserver/task.c
index 3eeefb7f9..aca45a737 100644
--- a/src/libserver/task.c
+++ b/src/libserver/task.c
@@ -199,8 +199,6 @@ rspamd_task_restore (void *arg)
 void
 rspamd_task_free (struct rspamd_task *task)
 {
-	struct rspamd_mime_part *p;
-	struct rspamd_mime_text_part *tp;
 	struct rspamd_email_address *addr;
 	struct rspamd_lua_cached_entry *entry;
 	static guint free_iters = 0;
@@ -211,38 +209,6 @@ rspamd_task_free (struct rspamd_task *task)
 	if (task) {
 		debug_task ("free pointer %p", task);
 
-		for (i = 0; i < task->parts->len; i ++) {
-			p = g_ptr_array_index (task->parts, i);
-
-			if (p->raw_headers) {
-				g_hash_table_unref (p->raw_headers);
-			}
-
-			if (p->headers_order) {
-				g_queue_free (p->headers_order);
-			}
-
-			if (IS_CT_MULTIPART (p->ct)) {
-				if (p->specific.mp->children) {
-					g_ptr_array_free (p->specific.mp->children, TRUE);
-				}
-			}
-		}
-
-		for (i = 0; i < task->text_parts->len; i ++) {
-			tp = g_ptr_array_index (task->text_parts, i);
-
-			if (tp->utf_words) {
-				g_array_free (tp->utf_words, TRUE);
-			}
-			if (tp->normalized_hashes) {
-				g_array_free (tp->normalized_hashes, TRUE);
-			}
-			if (tp->languages) {
-				g_ptr_array_unref (tp->languages);
-			}
-		}
-
 		if (task->rcpt_envelope) {
 			for (i = 0; i < task->rcpt_envelope->len; i ++) {
 				addr = g_ptr_array_index (task->rcpt_envelope, i);


More information about the Commits mailing list