]> source.dussan.org Git - rspamd.git/commitdiff
Allow owning lua_text.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 20 May 2015 11:03:46 +0000 (12:03 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 20 May 2015 11:03:46 +0000 (12:03 +0100)
src/lua/lua_common.h
src/lua/lua_mimepart.c
src/lua/lua_task.c
src/lua/lua_tcp.c

index 92c03c05a782f51b5544e60802a5bed44383208e..60bb6e44688b427c44f4ff37ce18019f3cd622db 100644 (file)
@@ -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 {
index 354dd3824f767a3b1394cda9268f10fa532c17f3..085295b183bd36963acd0b3d8c709d0499c0e621 100644 (file)
@@ -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;
 }
index d8f14a243856b319980eb425f37078da2d4fff68..d23f9143b2db4576b1f9d8bda948f90d5a780af5 100644 (file)
@@ -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
index 9d204daa82116072264aab288904e8dec00d85f8..67110c3ed23238fee4652ebd36e3676544d8b277 100644 (file)
@@ -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));