From: Vsevolod Stakhov Date: Fri, 17 Mar 2023 11:45:35 +0000 (+0000) Subject: [Minor] Add safe-guard for a number of regular expressions to be cached X-Git-Tag: 3.5~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=044a7abd8a9ba2e63d80438609b5a36cb6c866d6;p=thirdparty%2Frspamd.git [Minor] Add safe-guard for a number of regular expressions to be cached --- diff --git a/src/libutil/regexp.c b/src/libutil/regexp.c index 70a50872a3..e5610aa627 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 239df03b4c..94cc3d28e3 100644 --- a/src/libutil/regexp.h +++ b/src/libutil/regexp.h @@ -209,17 +209,6 @@ rspamd_regexp_t *rspamd_regexp_cache_query (struct rspamd_regexp_cache *cache, const gchar *pattern, 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)