From 2dafca29378d0bd77d972f87bf2c8999e8fec14f Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 20 May 2015 12:03:46 +0100 Subject: [PATCH] Allow owning lua_text. --- src/lua/lua_common.h | 3 ++- src/lua/lua_mimepart.c | 2 ++ src/lua/lua_task.c | 16 ++++++++++++++++ src/lua/lua_tcp.c | 1 + 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index 92c03c05a..60bb6e446 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -64,7 +64,8 @@ struct rspamd_lua_ip { struct rspamd_lua_text { const gchar *start; - gsize len; + guint len; + gboolean own; }; struct rspamd_lua_url { diff --git a/src/lua/lua_mimepart.c b/src/lua/lua_mimepart.c index 354dd3824..085295b18 100644 --- a/src/lua/lua_mimepart.c +++ b/src/lua/lua_mimepart.c @@ -278,6 +278,7 @@ lua_textpart_get_content (lua_State * L) rspamd_lua_setclass (L, "rspamd{text}", -1); t->start = part->content->data; t->len = part->content->len; + t->own = FALSE; return 1; } @@ -478,6 +479,7 @@ lua_mimepart_get_content (lua_State * L) rspamd_lua_setclass (L, "rspamd{text}", -1); t->start = part->content->data; t->len = part->content->len; + t->own = FALSE; return 1; } diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index d8f14a243..d23f9143b 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -529,12 +529,14 @@ static const struct luaL_reg imagelib_m[] = { LUA_FUNCTION_DEF (text, len); LUA_FUNCTION_DEF (text, str); LUA_FUNCTION_DEF (text, ptr); +LUA_FUNCTION_DEF (text, gc); static const struct luaL_reg textlib_m[] = { LUA_INTERFACE_DEF (text, len), LUA_INTERFACE_DEF (text, str), LUA_INTERFACE_DEF (text, ptr), {"__tostring", lua_text_str}, + {"__gc", lua_text_gc}, {NULL, NULL} }; @@ -836,6 +838,7 @@ lua_task_get_content (lua_State * L) rspamd_lua_setclass (L, "rspamd{text}", -1); t->len = task->msg.len; t->start = task->msg.start; + t->own = FALSE; return 1; } @@ -1040,6 +1043,7 @@ lua_task_get_raw_headers (lua_State *L) rspamd_lua_setclass (L, "rspamd{text}", -1); t->start = task->raw_headers_str; t->len = strlen (t->start); + t->own = FALSE; } else { lua_pushnil (L); @@ -2022,6 +2026,18 @@ lua_text_ptr (lua_State *L) return 1; } +static gint +lua_text_gc (lua_State *L) +{ + struct rspamd_lua_text *t = lua_check_text (L, 1); + + if (t != NULL && t->own) { + g_free ((gpointer)t->start); + } + + return 0; +} + /* Init part */ static gint diff --git a/src/lua/lua_tcp.c b/src/lua/lua_tcp.c index 9d204daa8..67110c3ed 100644 --- a/src/lua/lua_tcp.c +++ b/src/lua/lua_tcp.c @@ -150,6 +150,7 @@ lua_tcp_push_data (struct lua_tcp_cbdata *cbd, const gchar *str, gsize len) rspamd_lua_setclass (cbd->L, "rspamd{text}", -1); t->start = str; t->len = len; + t->own = FALSE; if (lua_pcall (cbd->L, 2, 0, 0) != 0) { msg_info ("callback call failed: %s", lua_tostring (cbd->L, -1)); -- 2.39.5