commit 2802836: [Minor] Fix match limit feature in regexps

Vsevolod Stakhov vsevolod at highsecure.ru
Tue Aug 24 15:21:04 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-08-24 16:19:25 +0100
URL: https://github.com/rspamd/rspamd/commit/2802836b8a2fb32ed46f943d96d882633b714196 (HEAD -> master)

[Minor] Fix match limit feature in regexps

---
 src/libutil/regexp.c | 25 +++++++++++++++++++++++++
 src/libutil/regexp.h | 10 ++++++++++
 src/lua/lua_common.h |  1 -
 src/lua/lua_regexp.c | 21 ++-------------------
 4 files changed, 37 insertions(+), 20 deletions(-)

diff --git a/src/libutil/regexp.c b/src/libutil/regexp.c
index 584c0c5c9..46ed9a0df 100644
--- a/src/libutil/regexp.c
+++ b/src/libutil/regexp.c
@@ -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)
diff --git a/src/libutil/regexp.h b/src/libutil/regexp.h
index 1e98b7b3c..3437619d1 100644
--- a/src/libutil/regexp.h
+++ b/src/libutil/regexp.h
@@ -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
  */
diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h
index 66403ecfe..d9e4fcaec 100644
--- a/src/lua/lua_common.h
+++ b/src/lua/lua_common.h
@@ -126,7 +126,6 @@ struct rspamd_lua_regexp {
 	rspamd_regexp_t *re;
 	gchar *module;
 	gchar *re_pattern;
-	gsize match_limit;
 	gint re_flags;
 };
 
diff --git a/src/lua/lua_regexp.c b/src/lua/lua_regexp.c
index bd3706d16..92ed1d790 100644
--- a/src/lua/lua_regexp.c
+++ b/src/lua/lua_regexp.c
@@ -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;


More information about the Commits mailing list