diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-03-17 11:45:35 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-03-17 11:45:35 +0000 |
commit | 044a7abd8a9ba2e63d80438609b5a36cb6c866d6 (patch) | |
tree | e8f7a05ce5061d57070b971a1ad44678ca8d6410 | |
parent | 1999b5b9ed2006874582eba93cc6961559fa9ddb (diff) | |
download | rspamd-044a7abd8a9ba2e63d80438609b5a36cb6c866d6.tar.gz rspamd-044a7abd8a9ba2e63d80438609b5a36cb6c866d6.zip |
[Minor] Add safe-guard for a number of regular expressions to be cached
-rw-r--r-- | src/libutil/regexp.c | 30 | ||||
-rw-r--r-- | src/libutil/regexp.h | 11 |
2 files changed, 9 insertions, 32 deletions
diff --git a/src/libutil/regexp.c b/src/libutil/regexp.c index 70a50872a..e5610aa62 100644 --- a/src/libutil/regexp.c +++ b/src/libutil/regexp.c @@ -83,6 +83,7 @@ struct rspamd_regexp_cache { static struct rspamd_regexp_cache *global_re_cache = NULL; static gboolean can_jit = FALSE; static gboolean check_jit = TRUE; +static const int max_re_cache_size = 8192; #ifdef WITH_PCRE2 static pcre2_compile_context *pcre2_ctx = NULL; @@ -1086,32 +1087,19 @@ rspamd_regexp_cache_create (struct rspamd_regexp_cache *cache, if (res) { /* REF_RETAIN (res); */ - g_hash_table_insert (cache->tbl, res->id, res); + if (g_hash_table_size (cache->tbl) < max_re_cache_size) { + g_hash_table_insert(cache->tbl, res->id, res); + } + else { + msg_warn("cannot insert regexp to the cache: maximum size is reached (%d expressions); " + "it might be cached regexp misuse; regexp pattern: %s", + max_re_cache_size, pattern); + } } return res; } -void rspamd_regexp_cache_insert (struct rspamd_regexp_cache* cache, - const gchar *pattern, - const gchar *flags, rspamd_regexp_t *re) -{ - g_assert (re != NULL); - g_assert (pattern != NULL); - - if (cache == NULL) { - rspamd_regexp_library_init (NULL); - cache = global_re_cache; - } - - g_assert (cache != NULL); - /* Generate custom id */ - rspamd_regexp_generate_id (pattern, flags, re->id); - - REF_RETAIN (re); - g_hash_table_insert (cache->tbl, re->id, re); -} - gboolean rspamd_regexp_cache_remove (struct rspamd_regexp_cache *cache, rspamd_regexp_t *re) diff --git a/src/libutil/regexp.h b/src/libutil/regexp.h index 239df03b4..94cc3d28e 100644 --- a/src/libutil/regexp.h +++ b/src/libutil/regexp.h @@ -210,17 +210,6 @@ rspamd_regexp_t *rspamd_regexp_cache_query (struct rspamd_regexp_cache *cache, const gchar *flags); /** - * Insert item to the cache using custom pattern and flags - * @param cache - * @param pattern - * @param flags - * @param re - */ -void rspamd_regexp_cache_insert (struct rspamd_regexp_cache *cache, - const gchar *pattern, - const gchar *flags, rspamd_regexp_t *re); - -/** * Create or get cached regexp from the specified cache * @param cache regexp cache. if NULL, the superglobal cache is used (*not* thread-safe) * @param pattern regexp pattern |