Copyright updates:
[exim.git] / src / src / regex_cache.c
index 6ac134cd8d8debe8c74fce299d0084b8e417759c..3f02802d6461163b2048c7f6ba82bd2e27326489 100644 (file)
@@ -3,8 +3,9 @@
 *************************************************/
 
 /*
- * Copyright (c) The Exim Maintainers 2022
+ * Copyright (c) The Exim Maintainers 2022 - 2023
  * License: GPL
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 /* Caching layers for compiled REs.  There is a local layer in the process,
@@ -39,6 +40,8 @@ typedef struct re_req {
 static tree_node * regex_cache = NULL;
 static tree_node * regex_caseless_cache = NULL;
 
+#define REGEX_CACHESIZE_LIMIT 1000
+
 /******************************************************************************/
 
 static void
@@ -89,11 +92,9 @@ return node ? node->data.ptr : NULL;
 static void
 regex_to_cache(const uschar * key, BOOL caseless, const pcre2_code * cre)
 {
-PCRE2_SIZE srelen;
-uschar * sre;
-tree_node * node;
 
-node = store_get(sizeof(tree_node) + Ustrlen(key) + 1, key);   /* we are called with STORE_PERM */
+/* we are called with STORE_PERM */
+tree_node * node = store_get(sizeof(tree_node) + Ustrlen(key) + 1, key);
 Ustrcpy(node->name, key);
 node->data.ptr = (void *)cre;
 
@@ -236,9 +237,14 @@ regex_at_daemon(const uschar * reqbuf)
 {
 const re_req * req = (const re_req *)reqbuf;
 uschar * errstr;
-const pcre2_code * cre = regex_compile(req->re,
-  req->caseless ? MCS_CASELESS | MCS_CACHEABLE : MCS_CACHEABLE,
-  &errstr, pcre_gen_cmp_ctx);
+const pcre2_code * cre = NULL;
+
+if (regex_cachesize >= REGEX_CACHESIZE_LIMIT)
+  errstr = US"regex cache size limit reached";
+else if ((cre = regex_compile(req->re,
+           req->caseless ? MCS_CASELESS | MCS_CACHEABLE : MCS_CACHEABLE,
+           &errstr, pcre_gen_cmp_ctx)))
+  regex_cachesize++;
 
 DEBUG(D_any) if (!cre) debug_printf("%s\n", errstr);
 return;