diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-04-14 15:07:00 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-04-14 15:07:00 +0100 |
commit | 00a4376019675cdbde59f1f0727580372c60f51b (patch) | |
tree | 2826853b33bb9daaace39506196ac4f426575c3b /src | |
parent | 8d84fd154d87a84eb2775f2d7ff7ca0868c30e10 (diff) | |
download | rspamd-00a4376019675cdbde59f1f0727580372c60f51b.tar.gz rspamd-00a4376019675cdbde59f1f0727580372c60f51b.zip |
Fix regexps lifetime.
Diffstat (limited to 'src')
-rw-r--r-- | src/libutil/regexp.c | 11 | ||||
-rw-r--r-- | src/lua/lua_regexp.c | 49 |
2 files changed, 48 insertions, 12 deletions
diff --git a/src/libutil/regexp.c b/src/libutil/regexp.c index 4ecfa7879..120175016 100644 --- a/src/libutil/regexp.c +++ b/src/libutil/regexp.c @@ -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) { diff --git a/src/lua/lua_regexp.c b/src/lua/lua_regexp.c index b477d3f9b..34c240989 100644 --- a/src/lua/lua_regexp.c +++ b/src/lua/lua_regexp.c @@ -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; |