From: Vsevolod Stakhov Date: Sat, 17 Jun 2023 11:19:01 +0000 (+0100) Subject: [Minor] Allow to allocate rspamd_text from task X-Git-Tag: 3.6~73 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=99c748761b602296cb3d23a0843c68c9f7b496b5;p=thirdparty%2Frspamd.git [Minor] Allow to allocate rspamd_text from task --- diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index 2ea51249d4..226b250a7b 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 f19d424fea..8e25c34900 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 bafab3c083..f911b3e6fd 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) {