]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Convert mime.conf regex matching to std::regex
authorAmos Jeffries <squid3@treenet.co.nz>
Mon, 27 Jul 2015 20:30:22 +0000 (13:30 -0700)
committerAmos Jeffries <squid3@treenet.co.nz>
Mon, 27 Jul 2015 20:30:22 +0000 (13:30 -0700)
src/mime.cc

index ec8d77d11f879e836cee18b7df48cb573b9bbb3a..eac260b5c0b3c130bfe2dfcc525d39b46088531b 100644 (file)
@@ -9,6 +9,7 @@
 /* DEBUG: section 25    MIME Parsing and Internal Icons */
 
 #include "squid.h"
+#include "base/RegexPattern.h"
 #include "disk.h"
 #include "fde.h"
 #include "globals.h"
@@ -56,15 +57,16 @@ class MimeEntry
     MEMPROXY_CLASS(MimeEntry);
 
 public:
-    explicit MimeEntry(const char *aPattern, const regex_t &compiledPattern,
+    MimeEntry(const char *aPattern, const decltype(RegexPattern::flags) &reFlags,
                        const char *aContentType,
                        const char *aContentEncoding, const char *aTransferMode,
                        bool optionViewEnable, bool optionDownloadEnable,
                        const char *anIconName);
+    MimeEntry(const MimeEntry &) = delete;
+    MimeEntry(const MimeEntry &&) = delete;
     ~MimeEntry();
 
-    const char *pattern;
-    regex_t compiled_pattern;
+    RegexPattern pattern;
     const char *content_type;
     const char *content_encoding;
     char transfer_mode;
@@ -88,7 +90,7 @@ mimeGetEntry(const char *fn, int skip_encodings)
         t = NULL;
 
         for (m = MimeTable; m; m = m->next) {
-            if (regexec(&m->compiled_pattern, name, 0, 0, 0) == 0)
+            if (m->pattern.match(name))
                 break;
         }
 
@@ -231,7 +233,6 @@ mimeInit(char *filename)
     char buf[BUFSIZ];
     char chopbuf[BUFSIZ];
     char *t;
-    char *pattern;
     char *icon;
     char *type;
     char *encoding;
@@ -239,9 +240,7 @@ mimeInit(char *filename)
     char *option;
     int view_option;
     int download_option;
-    regex_t re;
     MimeEntry *m;
-    int re_flags = REG_EXTENDED | REG_NOSUB | REG_ICASE;
 
     if (filename == NULL)
         return;
@@ -257,6 +256,8 @@ mimeInit(char *filename)
 
     mimeFreeMemory();
 
+    const auto re_flags = std::regex::extended | std::regex::nosubs | std::regex::icase;
+
     while (fgets(buf, BUFSIZ, fp)) {
         if ((t = strchr(buf, '#')))
             *t = '\0';
@@ -272,6 +273,7 @@ mimeInit(char *filename)
 
         xstrncpy(chopbuf, buf, BUFSIZ);
 
+        char *pattern;
         if ((pattern = strtok(chopbuf, w_space)) == NULL) {
             debugs(25, DBG_IMPORTANT, "mimeInit: parse error: '" << buf << "'");
             continue;
@@ -309,14 +311,14 @@ mimeInit(char *filename)
                 debugs(25, DBG_IMPORTANT, "mimeInit: unknown option: '" << buf << "' (" << option << ")");
         }
 
-        if (regcomp(&re, pattern, re_flags) != 0) {
-            debugs(25, DBG_IMPORTANT, "mimeInit: regcomp error: '" << buf << "'");
+        try {
+            m = new MimeEntry(pattern, re_flags, type, encoding, mode, view_option, download_option, icon);
+
+        } catch (std::regex_error &e) {
+            debugs(25, DBG_IMPORTANT, "mimeInit: invalid regular expression: '" << buf << "'");
             continue;
         }
 
-        m = new MimeEntry(pattern,re,type,encoding,mode,view_option,
-                          download_option,icon);
-
         *MimeTableTail = m;
 
         MimeTableTail = &m->next;
@@ -438,23 +440,21 @@ MimeIcon::created(StoreEntry *newEntry)
 
 MimeEntry::~MimeEntry()
 {
-    xfree(pattern);
     xfree(content_type);
     xfree(content_encoding);
-    regfree(&compiled_pattern);
 }
 
-MimeEntry::MimeEntry(const char *aPattern, const regex_t &compiledPattern,
+MimeEntry::MimeEntry(const char *aPattern, const decltype(RegexPattern::flags) &reFlags,
                      const char *aContentType, const char *aContentEncoding,
                      const char *aTransferMode, bool optionViewEnable,
                      bool optionDownloadEnable, const char *anIconName) :
-    pattern(xstrdup(aPattern)),
-    compiled_pattern(compiledPattern),
+    pattern(reFlags, aPattern),
     content_type(xstrdup(aContentType)),
     content_encoding(xstrdup(aContentEncoding)),
     view_option(optionViewEnable),
     download_option(optionDownloadEnable),
-    theIcon(anIconName), next(NULL)
+    theIcon(anIconName),
+    next(nullptr)
 {
     if (!strcasecmp(aTransferMode, "ascii"))
         transfer_mode = 'A';