]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
Fix regexps lifetime.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 14 Apr 2015 14:07:00 +0000 (15:07 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 14 Apr 2015 14:07:00 +0000 (15:07 +0100)
src/libutil/regexp.c
src/lua/lua_regexp.c

index 4ecfa7879db7101561396e3ae9f07d134358c5e4..120175016d47df4eba5e314f8a3991cf1243142d 100644 (file)
@@ -86,6 +86,7 @@ static void
 rspamd_regexp_dtor (rspamd_regexp_t *re)
 {
        if (re) {
+               msg_info("dtor of %s", re->pattern);
                if (re->re) {
                        pcre_free (re->re);
 #ifdef HAVE_PCRE_JIT
@@ -451,6 +452,16 @@ rspamd_regexp_unref (rspamd_regexp_t *re)
        REF_RELEASE (re);
 }
 
+rspamd_regexp_t*
+rspamd_regexp_ref (rspamd_regexp_t *re)
+{
+       g_assert (re != NULL);
+
+       REF_RETAIN (re);
+
+       return re;
+}
+
 void
 rspamd_regexp_set_ud (rspamd_regexp_t *re, gpointer ud)
 {
index b477d3f9ba08a15a41d5ff13c8621f076721c125..34c240989b35cc054ef2652ee872411640673db2 100644 (file)
@@ -108,7 +108,7 @@ lua_regexp_create (lua_State *L)
                flags_str = luaL_checkstring (L, 2);
        }
 
-       re = rspamd_regexp_cache_create (NULL, string, flags_str, &err);
+       re = rspamd_regexp_new (string, flags_str, &err);
        if (re == NULL) {
                lua_pushnil (L);
                msg_info ("cannot parse regexp: %s, error: %s",
@@ -138,15 +138,20 @@ lua_regexp_create (lua_State *L)
 static int
 lua_regexp_get_cached (lua_State *L)
 {
-       struct rspamd_lua_regexp *new, **pnew;
-       const gchar *line;
        rspamd_regexp_t *re;
+       struct rspamd_lua_regexp *new, **pnew;
+       const gchar *string, *flags_str = NULL;
+
+       string = luaL_checkstring (L, 1);
+       if (lua_gettop (L) == 2) {
+               flags_str = luaL_checkstring (L, 2);
+       }
+
+       re = rspamd_regexp_cache_query (NULL, string, flags_str);
 
-       line = luaL_checkstring (L, 1);
-       re = rspamd_regexp_cache_query (NULL, line, NULL);
        if (re) {
                new = g_slice_alloc0 (sizeof (struct rspamd_lua_regexp));
-               new->re = re;
+               new->re = rspamd_regexp_ref (re);
                pnew = lua_newuserdata (L, sizeof (struct rspamd_lua_regexp *));
                rspamd_lua_setclass (L, "rspamd{regexp}", -1);
                *pnew = new;
@@ -176,22 +181,42 @@ lua_regexp_get_cached (lua_State *L)
 static int
 lua_regexp_create_cached (lua_State *L)
 {
-       const gchar *line;
-       struct rspamd_lua_regexp *new, **pnew;
        rspamd_regexp_t *re;
+       struct rspamd_lua_regexp *new, **pnew;
+       const gchar *string, *flags_str = NULL;
+       GError *err = NULL;
+
+       string = luaL_checkstring (L, 1);
+       if (lua_gettop (L) == 2) {
+               flags_str = luaL_checkstring (L, 2);
+       }
+
+       re = rspamd_regexp_cache_query (NULL, string, flags_str);
 
-       line = luaL_checkstring (L, 1);
-       re = rspamd_regexp_cache_query (NULL, line, NULL);
        if (re) {
                new = g_slice_alloc0 (sizeof (struct rspamd_lua_regexp));
-               new->re = re;
+               new->re = rspamd_regexp_ref (re);
                pnew = lua_newuserdata (L, sizeof (struct rspamd_lua_regexp *));
 
                rspamd_lua_setclass (L, "rspamd{regexp}", -1);
                *pnew = new;
        }
        else {
-               return lua_regexp_create (L);
+               re = rspamd_regexp_cache_create (NULL, string, flags_str, &err);
+               if (re == NULL) {
+                       lua_pushnil (L);
+                       msg_info ("cannot parse regexp: %s, error: %s",
+                                       string,
+                                       err == NULL ? "undefined" : err->message);
+                       g_error_free (err);
+               }
+               else {
+                       new = g_slice_alloc0 (sizeof (struct rspamd_lua_regexp));
+                       new->re = rspamd_regexp_ref (re);
+                       pnew = lua_newuserdata (L, sizeof (struct rspamd_lua_regexp *));
+                       rspamd_lua_setclass (L, "rspamd{regexp}", -1);
+                       *pnew = new;
+               }
        }
 
        return 1;