commit 99c7487: [Minor] Allow to allocate rspamd_text from task

Vsevolod Stakhov vsevolod at rspamd.com
Sat Jun 17 11:21:03 UTC 2023


Author: Vsevolod Stakhov
Date: 2023-06-17 12:19:01 +0100
URL: https://github.com/rspamd/rspamd/commit/99c748761b602296cb3d23a0843c68c9f7b496b5 (HEAD -> master)

[Minor] Allow to allocate rspamd_text from task

---
 src/lua/lua_common.h | 20 +++++++++++++++++++-
 src/lua/lua_task.c   | 13 ++++++-------
 src/lua/lua_text.c   | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 60 insertions(+), 8 deletions(-)

diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h
index 2ea51249d..226b250a7 100644
--- a/src/lua/lua_common.h
+++ b/src/lua/lua_common.h
@@ -255,9 +255,27 @@ struct rspamd_lua_text *lua_check_text (lua_State *L, gint pos);
 * @return
 */
 struct rspamd_lua_text *lua_check_text_or_string (lua_State *L, gint pos);
-/* Creates and *pushes* new rspamd text, data is copied if  RSPAMD_TEXT_FLAG_OWN is in flags*/
+/**
+ * Create new text object
+ * @param L
+ * @param start
+ * @param len
+ * @param own
+ * @return
+ */
 struct rspamd_lua_text *lua_new_text (lua_State *L, const gchar *start,
 									  gsize len, gboolean own);
+/**
+ * Create new text object from task pool if allocation is needed
+ * @param task
+ * @param L
+ * @param start
+ * @param len
+ * @param own
+ * @return
+ */
+struct rspamd_lua_text * lua_new_text_task(lua_State *L, struct rspamd_task *task,
+										   const gchar *start, gsize len, gboolean own);
 /**
  * Checks if a text has binary characters (non ascii and non-utf8 characters)
  * @param t
diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c
index f19d424fe..8e25c3490 100644
--- a/src/lua/lua_task.c
+++ b/src/lua/lua_task.c
@@ -2728,17 +2728,16 @@ lua_task_get_rawbody (lua_State * L)
 
 	if (task) {
 		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;
+				t = lua_new_text_task (L, task, task->msg.begin + MESSAGE_FIELD (task, raw_headers_content).len,
+					task->msg.len - MESSAGE_FIELD (task, raw_headers_content).len, FALSE);
 			}
 			else {
-				t->len = task->msg.len;
-				t->start = task->msg.begin;
+				t = lua_new_text_task (L, task, task->msg.begin,
+					task->msg.len, FALSE);
 			}
 
 			t->flags = 0;
@@ -2746,7 +2745,7 @@ lua_task_get_rawbody (lua_State * L)
 		else {
 			/* Push body it it is there */
 			if (task->msg.len > 0 && task->msg.begin != NULL) {
-				lua_new_text (L, task->msg.begin, task->msg.len, FALSE);
+				lua_new_text_task (L, task->msg.begin, task->msg.len, FALSE);
 			}
 			else {
 				lua_pushnil (L);
diff --git a/src/lua/lua_text.c b/src/lua/lua_text.c
index bafab3c08..f911b3e6f 100644
--- a/src/lua/lua_text.c
+++ b/src/lua/lua_text.c
@@ -348,6 +348,41 @@ lua_new_text (lua_State *L, const gchar *start, gsize len, gboolean own)
 	return t;
 }
 
+struct rspamd_lua_text *
+lua_new_text_task (lua_State *L, struct rspamd_task *task,
+				   const gchar *start, gsize len, gboolean own)
+{
+	struct rspamd_lua_text *t;
+
+	t = lua_newuserdata (L, sizeof (*t));
+	t->flags = 0;
+
+	if (own) {
+		gchar *storage;
+
+		if (len > 0) {
+			storage = rspamd_mempool_alloc (task->task_pool, len);
+
+			if (start != NULL) {
+				memcpy (storage, start, len);
+			}
+
+			t->start = storage;
+		}
+		else {
+			t->start = "";
+		}
+	}
+	else {
+		t->start = start;
+	}
+
+	t->len = len;
+	rspamd_lua_setclass (L, "rspamd{text}", -1);
+
+	return t;
+}
+
 bool
 lua_is_text_binary(struct rspamd_lua_text *t)
 {


More information about the Commits mailing list