Browse Source

[Minor] Fix match limit feature in regexps

pull/3863/head
Vsevolod Stakhov 4 years ago
parent
commit
2802836b8a
  1. 25
      src/libutil/regexp.c
  2. 10
      src/libutil/regexp.h
  3. 1
      src/lua/lua_common.h
  4. 21
      src/lua/lua_regexp.c

25
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)

10
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
*/

1
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;
};

21
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;

Loading…
Cancel
Save