]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] Add safe-guard for a number of regular expressions to be cached
authorVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 17 Mar 2023 11:45:35 +0000 (11:45 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 17 Mar 2023 11:45:35 +0000 (11:45 +0000)
src/libutil/regexp.c
src/libutil/regexp.h

index 70a50872a3481088241742c0989fdf89fad2c8b2..e5610aa627690a5aeedfa936b482a1c99688cb33 100644 (file)
@@ -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)
index 239df03b4c79f04f2451c5a1b59554fb88299f73..94cc3d28e3b40a97226b3a04b893861342d7041d 100644 (file)
@@ -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)