]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Fix match limit feature in regexps
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 24 Aug 2021 15:19:25 +0000 (16:19 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 24 Aug 2021 15:19:25 +0000 (16:19 +0100)
src/libutil/regexp.c
src/libutil/regexp.h
src/lua/lua_common.h
src/lua/lua_regexp.c

index 584c0c5c97b6dff309ff8cf8353d85c374fa6b1e..46ed9a0df18b5871067ecabf511c5060ce84c04c 100644 (file)
@@ -66,6 +66,7 @@ struct rspamd_regexp_s {
        gpointer ud;
        gpointer re_class;
        guint64 cache_id;
+       gsize match_limit;
        guint max_hits;
        gint flags;
        gint pcre_flags;
@@ -567,6 +568,10 @@ rspamd_regexp_search (const rspamd_regexp_t *re, const gchar *text, gsize len,
                len = strlen (text);
        }
 
+       if (re->match_limit > 0 && len > re->match_limit) {
+               len = re->match_limit;
+       }
+
        if (end != NULL && *end != NULL) {
                /* Incremental search */
                mt = (*end);
@@ -889,6 +894,26 @@ rspamd_regexp_set_cache_id (rspamd_regexp_t *re, guint64 id)
        return old;
 }
 
+gsize
+rspamd_regexp_get_match_limit (const rspamd_regexp_t *re)
+{
+       g_assert (re != NULL);
+
+       return re->match_limit;
+}
+
+gsize
+rspamd_regexp_set_match_limit (rspamd_regexp_t *re, gsize lim)
+{
+       gsize old;
+
+       g_assert (re != NULL);
+       old = re->match_limit;
+       re->match_limit = lim;
+
+       return old;
+}
+
 gboolean
 rspamd_regexp_match (const rspamd_regexp_t *re, const gchar *text, gsize len,
                gboolean raw)
index 1e98b7b3c5c6864840c59f0f9033ffc668bd8e8b..3437619d12d1ec1d9a2c8c2331f388205d4e2266 100644 (file)
@@ -181,6 +181,16 @@ guint64 rspamd_regexp_get_cache_id (const rspamd_regexp_t *re);
  */
 guint64 rspamd_regexp_set_cache_id (rspamd_regexp_t *re, guint64 id);
 
+/**
+ * Returns match limit for a regexp
+ */
+gsize rspamd_regexp_get_match_limit (const rspamd_regexp_t *re);
+
+/**
+ * Sets cache id for a regexp
+ */
+gsize rspamd_regexp_set_match_limit (rspamd_regexp_t *re, gsize lim);
+
 /**
  * Get regexp class for the re object
  */
index 66403ecfeae1dd4c1892091a14b1a3bac3e4b730..d9e4fcaece7269a0026a717d890d9ead6827c366 100644 (file)
@@ -126,7 +126,6 @@ struct rspamd_lua_regexp {
        rspamd_regexp_t *re;
        gchar *module;
        gchar *re_pattern;
-       gsize match_limit;
        gint re_flags;
 };
 
index bd3706d16d775c52499e4ac5837387f27d1f56c0..92ed1d7907b371a84345de7f6cedfde9706b590b 100644 (file)
@@ -399,10 +399,10 @@ lua_regexp_set_limit (lua_State *L)
 
        if (re && re->re && !IS_DESTROYED (re)) {
                if (lim > 0) {
-                       re->match_limit = lim;
+                       rspamd_regexp_set_match_limit(re->re, lim);
                }
                else {
-                       re->match_limit = 0;
+                       rspamd_regexp_set_match_limit(re->re, 0);
                }
        }
 
@@ -522,10 +522,6 @@ lua_regexp_search (lua_State *L)
                        lua_newtable (L);
                        i = 0;
 
-                       if (re->match_limit > 0) {
-                               len = MIN (len, re->match_limit);
-                       }
-
                        while (rspamd_regexp_search (re->re, data, len, &start, &end, raw,
                                        captures)) {
 
@@ -605,10 +601,6 @@ lua_regexp_match (lua_State *L)
                }
 
                if (data && len > 0) {
-                       if (re->match_limit > 0) {
-                               len = MIN (len, re->match_limit);
-                       }
-
                        if (rspamd_regexp_search (re->re, data, len, NULL, NULL, raw, NULL)) {
                                lua_pushboolean (L, TRUE);
                        }
@@ -670,11 +662,6 @@ lua_regexp_matchn (lua_State *L)
                }
 
                if (data && len > 0) {
-
-                       if (re->match_limit > 0) {
-                               len = MIN (len, re->match_limit);
-                       }
-
                        for (;;) {
                                if (rspamd_regexp_search (re->re, data, len, &start, &end, raw,
                                                NULL)) {
@@ -740,10 +727,6 @@ lua_regexp_split (lua_State *L)
                        is_text = TRUE;
                }
 
-               if (re->match_limit > 0) {
-                       len = MIN (len, re->match_limit);
-               }
-
                if (data && len > 0) {
                        lua_newtable (L);
                        i = 0;