Use pool storage for regex operations
[exim.git] / src / src / regex.c
index 2b3e308a2b9372b3eafd513fd9d72d1a1a3ec1b5..9b7b07405a78c1b3ac7f1436906026884f0b44ac 100644 (file)
@@ -2,9 +2,10 @@
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
-/* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-2015
+/*
+ * Copyright (c) The Exim Maintainers 2016 - 2022
+ * Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-2015
  * License: GPL
- * Copyright (c) The Exim Maintainers 2016 - 2021
  */
 
 /* Code for matching regular expressions against headers and body.
@@ -45,7 +46,7 @@ while ((regex_string = string_nextinlist(&list, &sep, NULL, 0)))
 
     /* compile our regular expression */
     if (!(re = pcre2_compile( (PCRE2_SPTR) regex_string, PCRE2_ZERO_TERMINATED,
-                 0, &err, &pcre_erroffset, pcre_cmp_ctx)))
+                 0, &err, &pcre_erroffset, pcre_gen_cmp_ctx)))
       {
       uschar errbuf[128];
       pcre2_get_error_message(err, errbuf, sizeof(errbuf));
@@ -55,7 +56,7 @@ while ((regex_string = string_nextinlist(&list, &sep, NULL, 0)))
       continue;
       }
 
-    ri = store_get(sizeof(pcre_list), FALSE);
+    ri = store_get(sizeof(pcre_list), GET_UNTAINTED);
     ri->re = re;
     ri->pcre_text = regex_string;
     ri->next = re_list_head;
@@ -74,7 +75,7 @@ for (pcre_list * ri = re_list_head; ri; ri = ri->next)
   int n;
 
   /* try matcher on the line */
-  if ((n = pcre2_match(ri->re, (PCRE2_SPTR)linebuffer, len, 0, 0, md, pcre_mtc_ctx)) > 0)
+  if ((n = pcre2_match(ri->re, (PCRE2_SPTR)linebuffer, len, 0, 0, md, pcre_gen_mtc_ctx)) > 0)
     {
     Ustrncpy(regex_match_string_buffer, ri->pcre_text,
              sizeof(regex_match_string_buffer)-1);
@@ -84,14 +85,14 @@ for (pcre_list * ri = re_list_head; ri; ri = ri->next)
       {
       PCRE2_UCHAR * cstr;
       PCRE2_SIZE cslen;
-      pcre2_substring_get_bynumber(md, nn, &cstr, &cslen);
+      pcre2_substring_get_bynumber(md, nn, &cstr, &cslen);     /* uses same ctx as md */
       regex_vars[nn-1] = CUS cstr;
       }
 
     return OK;
     }
   }
-pcre2_match_data_free(md);
+/* pcre2_match_data_free(md);  gen ctx needs no free */
 return FAIL;
 }
 
@@ -133,7 +134,7 @@ if (!(re_list_head = compile(*listptr)))
   return FAIL;                 /* no regexes -> nothing to do */
 
 /* match each line against all regexes */
-linebuffer = store_get(32767, TRUE);   /* tainted */
+linebuffer = store_get(32767, GET_TAINTED);
 while (fgets(CS linebuffer, 32767, mbox_file))
   {
   if (  mime_stream && mime_current_boundary           /* check boundary */
@@ -204,7 +205,7 @@ if (!(f = fopen(CS mime_decoded_filename, "rb")))
   }
 
 /* get 32k memory, tainted */
-mime_subject = store_get(32767, TRUE);
+mime_subject = store_get(32767, GET_TAINTED);
 
 mime_subject_len = fread(mime_subject, 1, 32766, f);