commit 8e2facd: [Project] Empty messages cases handling

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


Author: Vsevolod Stakhov
Date: 2019-07-12 17:20:35 +0100
URL: https://github.com/rspamd/rspamd/commit/8e2facd1aaeaca370750185ba4b627e76140c02d

[Project] Empty messages cases handling

---
 src/libmime/filter.c         |  4 +-
 src/libmime/message.c        |  1 +
 src/libmime/message.h        |  3 ++
 src/libmime/mime_encoding.c  |  2 +-
 src/libmime/mime_headers.c   |  2 +-
 src/libserver/protocol.c     | 12 +++---
 src/libserver/roll_history.c |  6 ++-
 src/libserver/task.c         | 30 +++++++++------
 src/lua/lua_task.c           | 90 ++++++++++++++++++++++++--------------------
 9 files changed, 86 insertions(+), 64 deletions(-)

diff --git a/src/libmime/filter.c b/src/libmime/filter.c
index 83a9881d6..a193f5f78 100644
--- a/src/libmime/filter.c
+++ b/src/libmime/filter.c
@@ -139,14 +139,14 @@ rspamd_add_passthrough_result (struct rspamd_task *task,
 	if (!isnan (target_score)) {
 
 		msg_info_task ("<%s>: set pre-result to '%s' %s(%.2f): '%s' from %s(%d)",
-				MESSAGE_FIELD (task, message_id), action->name,
+				MESSAGE_FIELD_CHECK (task, message_id), action->name,
 				flags & RSPAMD_PASSTHROUGH_LEAST ? "*least " : "",
 				target_score,
 				message, module, priority);
 	}
 	else {
 		msg_info_task ("<%s>: set pre-result to '%s' %s(no score): '%s' from %s(%d)",
-				MESSAGE_FIELD (task, message_id), action->name,
+				MESSAGE_FIELD_CHECK (task, message_id), action->name,
 				flags & RSPAMD_PASSTHROUGH_LEAST ? "*least " : "",
 				message, module, priority);
 	}
diff --git a/src/libmime/message.c b/src/libmime/message.c
index fb488b71b..3efd30da0 100644
--- a/src/libmime/message.c
+++ b/src/libmime/message.c
@@ -1166,6 +1166,7 @@ rspamd_message_parse (struct rspamd_task *task)
 
 	if (RSPAMD_TASK_IS_EMPTY (task)) {
 		/* Don't do anything with empty task */
+		task->flags |= RSPAMD_TASK_FLAG_SKIP_PROCESS;
 		return TRUE;
 	}
 
diff --git a/src/libmime/message.h b/src/libmime/message.h
index cc4db39f4..a6b6f1022 100644
--- a/src/libmime/message.h
+++ b/src/libmime/message.h
@@ -158,6 +158,9 @@ struct rspamd_message {
 };
 
 #define MESSAGE_FIELD(task, field) ((task)->message->field)
+#define MESSAGE_FIELD_CHECK(task, field) ((task)->message ? \
+	(task)->message->field : \
+	(__typeof__((task)->message->field))NULL)
 
 /**
  * Parse and pre-process mime message
diff --git a/src/libmime/mime_encoding.c b/src/libmime/mime_encoding.c
index 4622ee032..0fbba54b2 100644
--- a/src/libmime/mime_encoding.c
+++ b/src/libmime/mime_encoding.c
@@ -668,7 +668,7 @@ rspamd_mime_text_part_maybe_convert (struct rspamd_task *task,
 
 	if (charset == NULL) {
 		msg_info_task ("<%s>: has invalid charset",
-				MESSAGE_FIELD (task, message_id));
+				MESSAGE_FIELD_CHECK (task, message_id));
 		SET_PART_RAW (text_part);
 		text_part->utf_raw_content = part_content;
 
diff --git a/src/libmime/mime_headers.c b/src/libmime/mime_headers.c
index 99936dd8c..7c6c02709 100644
--- a/src/libmime/mime_headers.c
+++ b/src/libmime/mime_headers.c
@@ -1621,7 +1621,7 @@ struct rspamd_mime_header *
 rspamd_message_get_header_array (struct rspamd_task *task,
 								 const gchar *field)
 {
-	return rspamd_message_get_header_from_hash (MESSAGE_FIELD (task, raw_headers),
+	return rspamd_message_get_header_from_hash (MESSAGE_FIELD_CHECK (task, raw_headers),
 			field);
 }
 
diff --git a/src/libserver/protocol.c b/src/libserver/protocol.c
index ff332d1cc..8834529ee 100644
--- a/src/libserver/protocol.c
+++ b/src/libserver/protocol.c
@@ -858,7 +858,7 @@ urls_protocol_cb (gpointer key, gpointer value, gpointer ud)
 		}
 
 		msg_notice_task_encrypted ("<%s> %s: %*s; ip: %s; URL: %*s",
-			MESSAGE_FIELD (task, message_id),
+			MESSAGE_FIELD_CHECK (task, message_id),
 			has_user ? "user" : "from",
 			len, user_field,
 			rspamd_inet_address_to_string (task->from_addr),
@@ -935,7 +935,7 @@ rspamd_protocol_rewrite_subject (struct rspamd_task *task)
 	}
 
 	p = c;
-	s = MESSAGE_FIELD (task, subject);
+	s = MESSAGE_FIELD_CHECK (task, subject);
 
 	if (s) {
 		slen = strlen (s);
@@ -1285,7 +1285,7 @@ rspamd_protocol_write_ucl (struct rspamd_task *task,
 		}
 	}
 
-	if (flags & RSPAMD_PROTOCOL_URLS) {
+	if (flags & RSPAMD_PROTOCOL_URLS && task->message) {
 		if (g_hash_table_size (MESSAGE_FIELD (task, urls)) > 0) {
 			ucl_object_insert_key (top,
 					rspamd_urls_tree_ucl (MESSAGE_FIELD (task, urls), task),
@@ -1307,7 +1307,7 @@ rspamd_protocol_write_ucl (struct rspamd_task *task,
 
 	if (flags & RSPAMD_PROTOCOL_BASIC) {
 		ucl_object_insert_key (top,
-				ucl_object_fromstring (MESSAGE_FIELD (task, message_id)),
+				ucl_object_fromstring (MESSAGE_FIELD_CHECK (task, message_id)),
 				"message-id", 0, false);
 		ucl_object_insert_key (top,
 				ucl_object_fromdouble (task->time_real_finish - task->task_timestamp),
@@ -1331,7 +1331,7 @@ rspamd_protocol_write_ucl (struct rspamd_task *task,
 					GString *folded_header;
 					dkim_sig = (GString *) dkim_sigs->data;
 
-					if (task->flags & RSPAMD_TASK_FLAG_MILTER) {
+					if (task->flags & RSPAMD_TASK_FLAG_MILTER || !task->message) {
 						folded_header = rspamd_header_value_fold ("DKIM-Signature",
 								dkim_sig->str, 80, RSPAMD_TASK_NEWLINES_LF, NULL);
 					}
@@ -1792,7 +1792,7 @@ rspamd_protocol_write_reply (struct rspamd_task *task, ev_tstamp timeout)
 
 	if (rspamd_http_connection_is_encrypted (task->http_conn)) {
 		msg_info_protocol ("<%s> writing encrypted reply",
-				MESSAGE_FIELD (task, message_id));
+				MESSAGE_FIELD_CHECK (task, message_id));
 	}
 
 	if (!RSPAMD_TASK_IS_JSON (task)) {
diff --git a/src/libserver/roll_history.c b/src/libserver/roll_history.c
index ee69d59d3..ceed8d04f 100644
--- a/src/libserver/roll_history.c
+++ b/src/libserver/roll_history.c
@@ -140,8 +140,10 @@ rspamd_roll_history_update (struct roll_history *history,
 	row->timestamp = task->task_timestamp;
 
 	/* Strings */
-	rspamd_strlcpy (row->message_id, MESSAGE_FIELD (task, message_id),
-		sizeof (row->message_id));
+	if (task->message) {
+		rspamd_strlcpy (row->message_id, MESSAGE_FIELD (task, message_id),
+				sizeof (row->message_id));
+	}
 	if (task->user) {
 		rspamd_strlcpy (row->user, task->user, sizeof (row->user));
 	}
diff --git a/src/libserver/task.c b/src/libserver/task.c
index dfad41220..950af5ec8 100644
--- a/src/libserver/task.c
+++ b/src/libserver/task.c
@@ -930,7 +930,7 @@ rspamd_task_get_principal_recipient (struct rspamd_task *task)
 		}
 	}
 
-	GPtrArray *rcpt_mime = MESSAGE_FIELD (task, rcpt_mime);
+	GPtrArray *rcpt_mime = MESSAGE_FIELD_CHECK (task, rcpt_mime);
 	if (rcpt_mime != NULL && rcpt_mime->len > 0) {
 		PTR_ARRAY_FOREACH (rcpt_mime, i, addr) {
 			if (addr->addr && !(addr->flags & RSPAMD_EMAIL_ADDR_ORIGINAL)) {
@@ -969,7 +969,7 @@ rspamd_task_log_check_condition (struct rspamd_task *task,
 
 	switch (lf->type) {
 	case RSPAMD_LOG_MID:
-		if (MESSAGE_FIELD (task, message_id) &&
+		if (MESSAGE_FIELD_CHECK (task, message_id) &&
 			strcmp (MESSAGE_FIELD (task, message_id) , "undef") != 0) {
 			ret = TRUE;
 		}
@@ -997,7 +997,7 @@ rspamd_task_log_check_condition (struct rspamd_task *task,
 		break;
 	case RSPAMD_LOG_MIME_RCPT:
 	case RSPAMD_LOG_MIME_RCPTS:
-		if (MESSAGE_FIELD (task, rcpt_mime) &&
+		if (MESSAGE_FIELD_CHECK (task, rcpt_mime) &&
 			MESSAGE_FIELD (task, rcpt_mime)->len > 0) {
 			ret = TRUE;
 		}
@@ -1008,7 +1008,7 @@ rspamd_task_log_check_condition (struct rspamd_task *task,
 		}
 		break;
 	case RSPAMD_LOG_MIME_FROM:
-		if (MESSAGE_FIELD (task, from_mime) &&
+		if (MESSAGE_FIELD_CHECK (task, from_mime) &&
 			MESSAGE_FIELD (task, from_mime)->len > 0) {
 			ret = TRUE;
 		}
@@ -1328,7 +1328,7 @@ rspamd_task_log_variable (struct rspamd_task *task,
 	switch (lf->type) {
 	/* String vars */
 	case RSPAMD_LOG_MID:
-		if (MESSAGE_FIELD (task, message_id)) {
+		if (MESSAGE_FIELD_CHECK (task, message_id)) {
 			var.begin = MESSAGE_FIELD (task, message_id);
 			var.len = strlen (var.begin);
 		}
@@ -1398,7 +1398,7 @@ rspamd_task_log_variable (struct rspamd_task *task,
 		}
 		break;
 	case RSPAMD_LOG_MIME_FROM:
-		if (MESSAGE_FIELD (task, from_mime)) {
+		if (MESSAGE_FIELD_CHECK (task, from_mime)) {
 			return rspamd_task_write_ialist (task,
 					MESSAGE_FIELD (task, from_mime),
 					1,
@@ -1413,7 +1413,7 @@ rspamd_task_log_variable (struct rspamd_task *task,
 		}
 		break;
 	case RSPAMD_LOG_MIME_RCPT:
-		if (MESSAGE_FIELD (task, rcpt_mime)) {
+		if (MESSAGE_FIELD_CHECK (task, rcpt_mime)) {
 			return rspamd_task_write_ialist (task,
 					MESSAGE_FIELD (task, rcpt_mime),
 					1,
@@ -1428,7 +1428,7 @@ rspamd_task_log_variable (struct rspamd_task *task,
 		}
 		break;
 	case RSPAMD_LOG_MIME_RCPTS:
-		if (MESSAGE_FIELD (task, rcpt_mime)) {
+		if (MESSAGE_FIELD_CHECK (task, rcpt_mime)) {
 			return rspamd_task_write_ialist (task,
 					MESSAGE_FIELD (task, rcpt_mime),
 					-1, /* All addresses */
@@ -1437,10 +1437,16 @@ rspamd_task_log_variable (struct rspamd_task *task,
 		}
 		break;
 	case RSPAMD_LOG_DIGEST:
-		var.len = rspamd_snprintf (numbuf, sizeof (numbuf), "%*xs",
-				(gint)sizeof (MESSAGE_FIELD (task, digest)),
-				MESSAGE_FIELD (task, digest));
-		var.begin = numbuf;
+		if (task->message) {
+			var.len = rspamd_snprintf (numbuf, sizeof (numbuf), "%*xs",
+					(gint) sizeof (MESSAGE_FIELD (task, digest)),
+					MESSAGE_FIELD (task, digest));
+			var.begin = numbuf;
+		}
+		else {
+			var.begin = undef;
+			var.len = sizeof (undef) - 1;
+		}
 		break;
 	case RSPAMD_LOG_FILENAME:
 		if (task->msg.fpath) {
diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c
index 1a4c8dc34..d77d5b01d 100644
--- a/src/lua/lua_task.c
+++ b/src/lua/lua_task.c
@@ -1271,7 +1271,10 @@ lua_task_set_cached (lua_State *L, struct rspamd_task *task, const gchar *key,
 	}
 
 	entry->ref = luaL_ref (L, LUA_REGISTRYINDEX);
-	memcpy (entry->id, MESSAGE_FIELD (task, digest), sizeof (entry->id));
+
+	if (task->message) {
+		memcpy (entry->id, MESSAGE_FIELD (task, digest), sizeof (entry->id));
+	}
 }
 
 
@@ -1283,8 +1286,9 @@ lua_task_get_cached (lua_State *L, struct rspamd_task *task, const gchar *key)
 
 	entry = g_hash_table_lookup (task->lua_cache, key);
 
-	if (entry != NULL && memcmp (entry->id, MESSAGE_FIELD (task, digest),
-			sizeof (entry->id)) == 0) {
+	if (entry != NULL && (task->message &&
+						  memcmp (entry->id, MESSAGE_FIELD (task, digest),
+								  sizeof (entry->id)) == 0)) {
 		lua_rawgeti (L, LUA_REGISTRYINDEX, entry->ref);
 
 		return TRUE;
@@ -1970,7 +1974,7 @@ lua_task_get_urls (lua_State * L)
 			PROTOCOL_FILE|PROTOCOL_FTP;
 	gsize sz;
 
-	if (task) {
+	if (task && task->message) {
 		if (lua_gettop (L) >= 2) {
 			if (lua_type (L, 2) == LUA_TBOOLEAN) {
 				protocols_mask = default_mask;
@@ -2087,7 +2091,7 @@ lua_task_has_urls (lua_State * L)
 	struct rspamd_task *task = lua_check_task (L, 1);
 	gboolean need_emails = FALSE, ret = FALSE;
 
-	if (task) {
+	if (task && task->message) {
 		if (lua_gettop (L) >= 2) {
 			need_emails = lua_toboolean (L, 2);
 		}
@@ -2159,20 +2163,25 @@ lua_task_get_rawbody (lua_State * L)
 	struct rspamd_lua_text *t;
 
 	if (task) {
-		t = lua_newuserdata (L, sizeof (*t));
-		rspamd_lua_setclass (L, "rspamd{text}", -1);
+		if (task->message != NULL) {
+			t = lua_newuserdata (L, sizeof (*t));
+			rspamd_lua_setclass (L, "rspamd{text}", -1);
+
+			if (MESSAGE_FIELD (task, raw_headers_content).len > 0) {
+				g_assert (MESSAGE_FIELD (task, raw_headers_content).len <= task->msg.len);
+				t->start = task->msg.begin + MESSAGE_FIELD (task, raw_headers_content).len;
+				t->len = task->msg.len - MESSAGE_FIELD (task, raw_headers_content).len;
+			}
+			else {
+				t->len = task->msg.len;
+				t->start = task->msg.begin;
+			}
 
-		if (MESSAGE_FIELD (task, raw_headers_content).len > 0) {
-			g_assert (MESSAGE_FIELD (task, raw_headers_content).len <= task->msg.len);
-			t->start = task->msg.begin + MESSAGE_FIELD (task, raw_headers_content).len;
-			t->len = task->msg.len - MESSAGE_FIELD (task, raw_headers_content).len;
+			t->flags = 0;
 		}
 		else {
-			t->len = task->msg.len;
-			t->start = task->msg.begin;
+			lua_pushnil (L);
 		}
-
-		t->flags = 0;
 	}
 	else {
 		return luaL_error (L, "invalid arguments");
@@ -2211,7 +2220,7 @@ lua_task_get_text_parts (lua_State * L)
 	struct rspamd_task *task = lua_check_task (L, 1);
 	struct rspamd_mime_text_part *part, **ppart;
 
-	if (task != NULL) {
+	if (task != NULL && task->message != NULL) {
 
 		if (!lua_task_get_cached (L, task, "text_parts")) {
 			lua_createtable (L, MESSAGE_FIELD (task, text_parts)->len, 0);
@@ -2242,7 +2251,7 @@ lua_task_get_parts (lua_State * L)
 	struct rspamd_task *task = lua_check_task (L, 1);
 	struct rspamd_mime_part *part, **ppart;
 
-	if (task != NULL) {
+	if (task != NULL && task->message != NULL) {
 		if (!lua_task_get_cached (L, task, "mime_parts")) {
 			lua_createtable (L, MESSAGE_FIELD (task, parts)->len, 0);
 
@@ -2519,7 +2528,7 @@ lua_task_get_raw_headers (lua_State *L)
 	struct rspamd_task *task = lua_check_task (L, 1);
 	struct rspamd_lua_text *t;
 
-	if (task) {
+	if (task && task->message) {
 		t = lua_newuserdata (L, sizeof (*t));
 		rspamd_lua_setclass (L, "rspamd{text}", -1);
 		t->start = MESSAGE_FIELD (task, raw_headers_content).begin;
@@ -2543,7 +2552,7 @@ lua_task_get_received_headers (lua_State * L)
 	const gchar *proto;
 	guint k = 1;
 
-	if (task) {
+	if (task && task->message) {
 		if (!lua_task_get_cached (L, task, "received")) {
 			lua_createtable (L, 0, 0);
 
@@ -3105,7 +3114,7 @@ lua_task_get_recipients (lua_State *L)
 			break;
 		case RSPAMD_ADDRESS_MIME:
 			/* Here we check merely mime rcpt */
-			ptrs = MESSAGE_FIELD (task, rcpt_mime);
+			ptrs = MESSAGE_FIELD_CHECK (task, rcpt_mime);
 			break;
 		case RSPAMD_ADDRESS_ANY:
 		default:
@@ -3113,7 +3122,7 @@ lua_task_get_recipients (lua_State *L)
 				ptrs = task->rcpt_envelope;
 			}
 			else {
-				ptrs = MESSAGE_FIELD (task, rcpt_mime);
+				ptrs = MESSAGE_FIELD_CHECK (task, rcpt_mime);
 			}
 			break;
 		}
@@ -3158,7 +3167,7 @@ lua_task_set_recipients (lua_State *L)
 			break;
 		case RSPAMD_ADDRESS_MIME:
 			/* Here we check merely mime rcpt */
-			ptrs = MESSAGE_FIELD (task, rcpt_mime);
+			ptrs = MESSAGE_FIELD_CHECK (task, rcpt_mime);
 			need_update_digest = TRUE;
 			break;
 		case RSPAMD_ADDRESS_ANY:
@@ -3167,7 +3176,7 @@ lua_task_set_recipients (lua_State *L)
 				ptrs = task->rcpt_envelope;
 			}
 			else {
-				ptrs = MESSAGE_FIELD (task, rcpt_mime);
+				ptrs = MESSAGE_FIELD_CHECK (task, rcpt_mime);
 				need_update_digest = TRUE;
 			}
 			break;
@@ -3251,14 +3260,14 @@ lua_task_has_from (lua_State *L)
 			break;
 		case RSPAMD_ADDRESS_MIME:
 			/* Here we check merely mime rcpt */
-			CHECK_EMAIL_ADDR_LIST (MESSAGE_FIELD (task, from_mime));
+			CHECK_EMAIL_ADDR_LIST (MESSAGE_FIELD_CHECK (task, from_mime));
 			break;
 		case RSPAMD_ADDRESS_ANY:
 		default:
 			CHECK_EMAIL_ADDR (task->from_envelope);
 
 			if (!ret) {
-				CHECK_EMAIL_ADDR_LIST (MESSAGE_FIELD (task, from_mime));
+				CHECK_EMAIL_ADDR_LIST (MESSAGE_FIELD_CHECK (task, from_mime));
 			}
 			break;
 		}
@@ -3293,14 +3302,14 @@ lua_task_has_recipients (lua_State *L)
 			break;
 		case RSPAMD_ADDRESS_MIME:
 			/* Here we check merely mime rcpt */
-			CHECK_EMAIL_ADDR_LIST (MESSAGE_FIELD (task, rcpt_mime));
+			CHECK_EMAIL_ADDR_LIST (MESSAGE_FIELD_CHECK (task, rcpt_mime));
 			break;
 		case RSPAMD_ADDRESS_ANY:
 		default:
 			CHECK_EMAIL_ADDR_LIST (task->rcpt_envelope);
 
 			if (!ret) {
-				CHECK_EMAIL_ADDR_LIST (MESSAGE_FIELD (task, rcpt_mime));
+				CHECK_EMAIL_ADDR_LIST (MESSAGE_FIELD_CHECK (task, rcpt_mime));
 			}
 			break;
 		}
@@ -3336,7 +3345,7 @@ lua_task_get_from (lua_State *L)
 			break;
 		case RSPAMD_ADDRESS_MIME:
 			/* Here we check merely mime rcpt */
-			addrs = MESSAGE_FIELD (task, from_mime);
+			addrs = MESSAGE_FIELD_CHECK (task, from_mime);
 			break;
 		case RSPAMD_ADDRESS_ANY:
 		default:
@@ -3344,7 +3353,7 @@ lua_task_get_from (lua_State *L)
 				addr = task->from_envelope;
 			}
 			else {
-				addrs = MESSAGE_FIELD (task, from_mime);
+				addrs = MESSAGE_FIELD_CHECK (task, from_mime);
 			}
 			break;
 		}
@@ -3399,7 +3408,7 @@ lua_task_set_from (lua_State *L)
 			break;
 		case RSPAMD_ADDRESS_MIME:
 			/* Here we check merely mime rcpt */
-			addrs = MESSAGE_FIELD (task, from_mime);
+			addrs = MESSAGE_FIELD_CHECK (task, from_mime);
 			need_update_digest = TRUE;
 			break;
 		case RSPAMD_ADDRESS_ANY:
@@ -3408,7 +3417,7 @@ lua_task_set_from (lua_State *L)
 				paddr = &task->from_envelope;
 			}
 			else {
-				addrs = MESSAGE_FIELD (task, from_mime);
+				addrs = MESSAGE_FIELD_CHECK (task, from_mime);
 				need_update_digest = TRUE;
 			}
 			break;
@@ -3501,7 +3510,7 @@ lua_task_get_reply_sender (lua_State *L)
 		if (rh) {
 			lua_pushstring (L, rh->decoded);
 		}
-		else if (MESSAGE_FIELD (task, from_mime) &&
+		else if (MESSAGE_FIELD_CHECK (task, from_mime) &&
 				MESSAGE_FIELD (task, from_mime)->len == 1) {
 			struct rspamd_email_address *addr;
 
@@ -3689,7 +3698,7 @@ lua_task_get_subject (lua_State *L)
 	struct rspamd_task *task = lua_check_task (L, 1);
 
 	if (task) {
-		if (MESSAGE_FIELD (task, subject) != NULL) {
+		if (MESSAGE_FIELD_CHECK (task, subject) != NULL) {
 			lua_pushstring (L, MESSAGE_FIELD (task, subject));
 			return 1;
 		}
@@ -3789,7 +3798,7 @@ lua_task_get_images (lua_State *L)
 	struct rspamd_mime_part *part;
 	struct rspamd_image **pimg;
 
-	if (task) {
+	if (task && task->message) {
 		if (!lua_task_get_cached (L, task, "images")) {
 			lua_createtable (L, MESSAGE_FIELD (task, parts)->len, 0);
 
@@ -3821,7 +3830,7 @@ lua_task_get_archives (lua_State *L)
 	struct rspamd_mime_part *part;
 	struct rspamd_archive **parch;
 
-	if (task) {
+	if (task && task->message) {
 		if (!lua_task_get_cached (L, task, "archives")) {
 			lua_createtable (L, MESSAGE_FIELD (task, parts)->len, 0);
 
@@ -4462,7 +4471,7 @@ lua_task_get_message_id (lua_State *L)
 	struct rspamd_task *task = lua_check_task (L, 1);
 
 	if (task != NULL) {
-		if (MESSAGE_FIELD (task, message_id) != NULL) {
+		if (MESSAGE_FIELD_CHECK (task, message_id) != NULL) {
 			lua_pushstring (L, MESSAGE_FIELD (task, message_id));
 		}
 		else {
@@ -4733,7 +4742,7 @@ lua_task_get_digest (lua_State *L)
 	gchar hexbuf[sizeof(MESSAGE_FIELD (task, digest)) * 2 + 1];
 	gint r;
 
-	if (task) {
+	if (task && task->message) {
 		r = rspamd_encode_hex_buf (MESSAGE_FIELD (task, digest),
 				sizeof (MESSAGE_FIELD (task, digest)),
 				hexbuf, sizeof (hexbuf) - 1);
@@ -5407,7 +5416,7 @@ lua_task_get_newlines_type (lua_State *L)
 	LUA_TRACE_POINT;
 	struct rspamd_task *task = lua_check_task (L, 1);
 
-	if (task) {
+	if (task && task->message) {
 		switch (MESSAGE_FIELD (task, nlines_type)) {
 		case RSPAMD_TASK_NEWLINES_CR:
 			lua_pushstring (L, "cr");
@@ -5630,7 +5639,7 @@ lua_task_headers_foreach (lua_State *L)
 	struct rspamd_mime_header *hdr, *cur;
 	gint old_top;
 
-	if (task && lua_isfunction (L, 2)) {
+	if (task && task->message && lua_isfunction (L, 2)) {
 		if (lua_istable (L, 3)) {
 			lua_pushstring (L, "full");
 			lua_gettable (L, 3);
@@ -5819,7 +5828,8 @@ lua_task_lookup_words (lua_State *L)
 
 	guint i, matches = 0;
 
-	if (task == NULL || map == NULL || lua_type (L, 3) != LUA_TFUNCTION) {
+	if (task == NULL || map == NULL || task->message == NULL
+		|| lua_type (L, 3) != LUA_TFUNCTION) {
 		return luaL_error (L, "invalid arguments");
 	}
 


More information about the Commits mailing list