commit 044a7ab: [Minor] Add safe-guard for a number of regular expressions to be cached

Vsevolod Stakhov vsevolod at rspamd.com
Fri Mar 17 11:49:04 UTC 2023


Author: Vsevolod Stakhov
Date: 2023-03-17 11:45:35 +0000
URL: https://github.com/rspamd/rspamd/commit/044a7abd8a9ba2e63d80438609b5a36cb6c866d6

[Minor] Add safe-guard for a number of regular expressions to be cached

---
 src/libutil/regexp.c | 30 +++++++++---------------------
 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
@@ -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)


More information about the Commits mailing list