]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect/pcre: Use local match variables 9243/head
authorJeff Lucovsky <jlucovsky@oisf.net>
Sun, 16 Jul 2023 15:57:14 +0000 (11:57 -0400)
committerVictor Julien <vjulien@oisf.net>
Mon, 17 Jul 2023 09:58:51 +0000 (11:58 +0200)
pcre2 is not thread-safe wrt match objects so use locally scoped
objects.

Issue: 4797

52 files changed:
src/detect-base64-decode.c
src/detect-byte-extract.c
src/detect-bytejump.c
src/detect-bytetest.c
src/detect-classtype.c
src/detect-config.c
src/detect-detection-filter.c
src/detect-engine-event.c
src/detect-fast-pattern.c
src/detect-filestore.c
src/detect-flow.c
src/detect-flowbits.c
src/detect-flowint.c
src/detect-flowvar.c
src/detect-fragbits.c
src/detect-fragoffset.c
src/detect-ftpdata.c
src/detect-hostbits.c
src/detect-icmp-id.c
src/detect-icmp-seq.c
src/detect-id.c
src/detect-ike-chosen-sa.c
src/detect-ipopts.c
src/detect-ipproto.c
src/detect-isdataat.c
src/detect-krb5-errcode.c
src/detect-krb5-msgtype.c
src/detect-mark.c
src/detect-mqtt-connect-flags.c
src/detect-mqtt-flags.c
src/detect-parse.c
src/detect-parse.h
src/detect-pktvar.c
src/detect-priority.c
src/detect-reference.c
src/detect-rfb-secresult.c
src/detect-rpc.c
src/detect-snmp-pdu_type.c
src/detect-ssh-proto-version.c
src/detect-ssh-software-version.c
src/detect-ssl-state.c
src/detect-tag.c
src/detect-target.c
src/detect-tcp-flags.c
src/detect-tcp-window.c
src/detect-template.c
src/detect-threshold.c
src/detect-tls-cert-validity.c
src/detect-tls-version.c
src/detect-tls.c
src/detect-tos.c
src/detect-xbits.c

index 0053a114e51384a1f01b1d572268507d6698256c..25fdf10e70c4508bf3453054229c2c1300b1ac76 100644 (file)
@@ -123,12 +123,9 @@ static int DetectBase64DecodeParse(const char *str, uint32_t *bytes,
     *offset = 0;
     *relative = 0;
     size_t pcre2_len;
+    pcre2_match_data *match = NULL;
 
-    pcre2_match_data *match = pcre2_match_data_create_from_pattern(decode_pcre.regex, NULL);
-    if (match == NULL)
-        goto error;
-
-    int pcre_rc = pcre2_match(decode_pcre.regex, (PCRE2_SPTR8)str, strlen(str), 0, 0, match, NULL);
+    int pcre_rc = DetectParsePcreExec(&decode_pcre, &match, str, 0, 0);
     if (pcre_rc < 3) {
         goto error;
     }
@@ -164,10 +161,13 @@ static int DetectBase64DecodeParse(const char *str, uint32_t *bytes,
         }
     }
 
+    retval = 1;
+
     pcre2_match_data_free(match);
     match = NULL;
-    retval = 1;
+
 error:
+
     if (bytes_str != NULL) {
         pcre2_substring_free((PCRE2_UCHAR8 *)bytes_str);
     }
@@ -177,7 +177,7 @@ error:
     if (relative_str != NULL) {
         pcre2_substring_free((PCRE2_UCHAR8 *)relative_str);
     }
-    if (match != NULL) {
+    if (match) {
         pcre2_match_data_free(match);
     }
     return retval;
index fd96a46e5ff96025c8781701cb4a275f9768b53f..ec9b27fc6406e11b3fd466bcb784de084e2f5578 100644 (file)
@@ -214,11 +214,12 @@ int DetectByteExtractDoMatch(DetectEngineThreadCtx *det_ctx, const SigMatchData
 static inline DetectByteExtractData *DetectByteExtractParse(DetectEngineCtx *de_ctx, const char *arg)
 {
     DetectByteExtractData *bed = NULL;
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2len;
     int i = 0;
+    pcre2_match_data *match = NULL;
 
-    ret = DetectParsePcreExec(&parse_regex, arg, 0, 0);
+    int ret = DetectParsePcreExec(&parse_regex, &match, arg, 0, 0);
     if (ret < 3 || ret > 19) {
         SCLogError("parse error, ret %" PRId32 ", string \"%s\"", ret, arg);
         SCLogError("Invalid arg to byte_extract : %s "
@@ -235,8 +236,7 @@ static inline DetectByteExtractData *DetectByteExtractParse(DetectEngineCtx *de_
     /* no of bytes to extract */
     char nbytes_str[64] = "";
     pcre2len = sizeof(nbytes_str);
-    res = pcre2_substring_copy_bynumber(
-            parse_regex.match, 1, (PCRE2_UCHAR8 *)nbytes_str, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)nbytes_str, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed "
                    "for arg 1 for byte_extract");
@@ -253,8 +253,7 @@ static inline DetectByteExtractData *DetectByteExtractParse(DetectEngineCtx *de_
     /* offset */
     char offset_str[64] = "";
     pcre2len = sizeof(offset_str);
-    res = pcre2_substring_copy_bynumber(
-            parse_regex.match, 2, (PCRE2_UCHAR8 *)offset_str, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)offset_str, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed "
                    "for arg 2 for byte_extract");
@@ -270,8 +269,7 @@ static inline DetectByteExtractData *DetectByteExtractParse(DetectEngineCtx *de_
     /* var name */
     char varname_str[256] = "";
     pcre2len = sizeof(varname_str);
-    res = pcre2_substring_copy_bynumber(
-            parse_regex.match, 3, (PCRE2_UCHAR8 *)varname_str, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 3, (PCRE2_UCHAR8 *)varname_str, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed "
                    "for arg 3 for byte_extract");
@@ -285,7 +283,7 @@ static inline DetectByteExtractData *DetectByteExtractParse(DetectEngineCtx *de_
     for (i = 4; i < ret; i++) {
         char opt_str[64] = "";
         pcre2len = sizeof(opt_str);
-        res = SC_Pcre2SubstringCopy(parse_regex.match, i, (PCRE2_UCHAR8 *)opt_str, &pcre2len);
+        res = SC_Pcre2SubstringCopy(match, i, (PCRE2_UCHAR8 *)opt_str, &pcre2len);
         if (res < 0) {
             SCLogError("pcre2_substring_copy_bynumber failed "
                        "for arg %d for byte_extract with %d",
@@ -312,7 +310,7 @@ static inline DetectByteExtractData *DetectByteExtractParse(DetectEngineCtx *de_
             char multiplier_str[16] = "";
             pcre2len = sizeof(multiplier_str);
             res = pcre2_substring_copy_bynumber(
-                    parse_regex.match, i, (PCRE2_UCHAR8 *)multiplier_str, &pcre2len);
+                    match, i, (PCRE2_UCHAR8 *)multiplier_str, &pcre2len);
             if (res < 0) {
                 SCLogError("pcre2_substring_copy_bynumber failed "
                            "for arg %d for byte_extract",
@@ -416,8 +414,7 @@ static inline DetectByteExtractData *DetectByteExtractParse(DetectEngineCtx *de_
 
             char align_str[16] = "";
             pcre2len = sizeof(align_str);
-            res = pcre2_substring_copy_bynumber(
-                    parse_regex.match, i, (PCRE2_UCHAR8 *)align_str, &pcre2len);
+            res = pcre2_substring_copy_bynumber(match, i, (PCRE2_UCHAR8 *)align_str, &pcre2len);
             if (res < 0) {
                 SCLogError("pcre2_substring_copy_bynumber failed "
                            "for arg %d in byte_extract",
@@ -507,10 +504,15 @@ static inline DetectByteExtractData *DetectByteExtractParse(DetectEngineCtx *de_
             bed->endian = DETECT_BYTE_EXTRACT_ENDIAN_DEFAULT;
     }
 
+    pcre2_match_data_free(match);
+
     return bed;
  error:
     if (bed != NULL)
         DetectByteExtractFree(de_ctx, bed);
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     return NULL;
 }
 
index 4054f049347e60392c147637a08175e6a6953746..63d70aad59193336aafad59be6d496bbcc7f879d 100644 (file)
@@ -372,18 +372,19 @@ static DetectBytejumpData *DetectBytejumpParse(
 {
     DetectBytejumpData *data = NULL;
     char args[10][64];
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2len;
     int numargs = 0;
     int i = 0;
     uint32_t nbytes = 0;
     char *str_ptr;
     char *end_ptr;
+    pcre2_match_data *match = NULL;
 
     memset(args, 0x00, sizeof(args));
 
     /* Execute the regex and populate args with captures. */
-    ret = DetectParsePcreExec(&parse_regex, optstr, 0, 0);
+    int ret = DetectParsePcreExec(&parse_regex, &match, optstr, 0, 0);
     if (ret < 2 || ret > 10) {
         SCLogError("parse error, ret %" PRId32 ", string \"%s\"", ret, optstr);
         goto error;
@@ -395,7 +396,7 @@ static DetectBytejumpData *DetectBytejumpParse(
      */
     char str[512] = "";
     pcre2len = sizeof(str);
-    res = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)str, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)str, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed "
                    "for arg 1");
@@ -425,8 +426,7 @@ static DetectBytejumpData *DetectBytejumpParse(
     /* The remaining args are directly from PCRE substrings */
     for (i = 1; i < (ret - 1); i++) {
         pcre2len = sizeof(args[0]);
-        res = pcre2_substring_copy_bynumber(
-                parse_regex.match, i + 1, (PCRE2_UCHAR8 *)args[i + 1], &pcre2len);
+        res = pcre2_substring_copy_bynumber(match, i + 1, (PCRE2_UCHAR8 *)args[i + 1], &pcre2len);
         if (res < 0) {
             SCLogError("pcre2_substring_copy_bynumber failed for arg %d", i + 1);
             goto error;
@@ -554,6 +554,7 @@ static DetectBytejumpData *DetectBytejumpParse(
         }
     }
 
+    pcre2_match_data_free(match);
     return data;
 
 error:
@@ -567,6 +568,9 @@ error:
     }
     if (data != NULL)
         DetectBytejumpFree(de_ctx, data);
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     return NULL;
 }
 
index d888664205a72ac49a9bae32612918ee96e31f75..3eeba0f0f0e61cd4872cad7eb7c7b97b15e1dc0a 100644 (file)
@@ -330,14 +330,15 @@ static DetectBytetestData *DetectBytetestParse(
     };
     char *test_value =  NULL;
     char *data_offset = NULL;
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2_len;
     int i;
     uint32_t nbytes;
     const char *str_ptr = NULL;
+    pcre2_match_data *match = NULL;
 
     /* Execute the regex and populate args with captures. */
-    ret = DetectParsePcreExec(&parse_regex, optstr, 0, 0);
+    int ret = DetectParsePcreExec(&parse_regex, &match, optstr, 0, 0);
     if (ret < 4 || ret > 9) {
         SCLogError("parse error, ret %" PRId32 ", string %s", ret, optstr);
         goto error;
@@ -345,8 +346,7 @@ static DetectBytetestData *DetectBytetestParse(
 
     /* Subtract two since two values  are conjoined */
     for (i = 0; i < (ret - 1); i++) {
-        res = pcre2_substring_get_bynumber(
-                parse_regex.match, i + 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+        res = pcre2_substring_get_bynumber(match, i + 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
         if (res < 0) {
             SCLogError("pcre2_substring_get_bynumber failed "
                        "for arg %d",
@@ -562,6 +562,7 @@ static DetectBytetestData *DetectBytetestParse(
     if (data_offset) SCFree(data_offset);
     if (test_value)
         pcre2_substring_free((PCRE2_UCHAR8 *)test_value);
+    pcre2_match_data_free(match);
     return data;
 
 error:
@@ -573,6 +574,9 @@ error:
     if (test_value)
         pcre2_substring_free((PCRE2_UCHAR8 *)test_value);
     if (data) SCFree(data);
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     return NULL;
 }
 
index 2815e65dd76d5db378639d3ba3d038dabac57207..bec179e72bc88d3ee85cf1f75642ae97eafe1542 100644 (file)
@@ -73,27 +73,35 @@ static int DetectClasstypeParseRawString(const char *rawstr, char *out, size_t o
 
     const size_t esize = CLASSTYPE_NAME_MAX_LEN + 8;
     char e[esize];
+    pcre2_match_data *match = NULL;
 
-    int ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     if (ret < 0) {
         SCLogError("Invalid Classtype in Signature");
-        return -1;
+        goto error;
     }
 
     pcre2len = esize;
-    ret = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)e, &pcre2len);
+    ret = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)e, &pcre2len);
     if (ret < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
-        return -1;
+        goto error;
     }
 
     if (strlen(e) >= CLASSTYPE_NAME_MAX_LEN) {
         SCLogError("classtype '%s' is too big: max %d", rawstr, CLASSTYPE_NAME_MAX_LEN - 1);
-        return -1;
+        goto error;
     }
     (void)strlcpy(out, e, outsize);
 
+    pcre2_match_data_free(match);
     return 0;
+
+error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
+    return -1;
 }
 
 /**
index e0f366e34984c3811329d49f327cb71b407b54ea..ae215dd2161cf1aaa27035be477c767a5f2e87bc 100644 (file)
@@ -171,7 +171,7 @@ static int DetectConfigSetup (DetectEngineCtx *de_ctx, Signature *s, const char
 
     DetectConfigData *fd = NULL;
     SigMatch *sm = NULL;
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2len;
 #if 0
     /* filestore and bypass keywords can't work together */
@@ -181,6 +181,7 @@ static int DetectConfigSetup (DetectEngineCtx *de_ctx, Signature *s, const char
         return -1;
     }
 #endif
+    pcre2_match_data *match = NULL;
     sm = SigMatchAlloc();
     if (sm == NULL)
         goto error;
@@ -198,13 +199,13 @@ static int DetectConfigSetup (DetectEngineCtx *de_ctx, Signature *s, const char
     char scopeval[32];
     SCLogDebug("str %s", str);
 
-    ret = DetectParsePcreExec(&parse_regex, str, 0, 0);
+    int ret = DetectParsePcreExec(&parse_regex, &match, str, 0, 0);
     if (ret != 7) {
         SCLogError("config is rather picky at this time");
         goto error;
     }
     pcre2len = sizeof(subsys);
-    res = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)subsys, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)subsys, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
@@ -217,7 +218,7 @@ static int DetectConfigSetup (DetectEngineCtx *de_ctx, Signature *s, const char
     SCLogDebug("subsys %s", subsys);
 
     pcre2len = sizeof(state);
-    res = pcre2_substring_copy_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 *)state, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)state, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
@@ -230,7 +231,7 @@ static int DetectConfigSetup (DetectEngineCtx *de_ctx, Signature *s, const char
     SCLogDebug("state %s", state);
 
     pcre2len = sizeof(type);
-    res = pcre2_substring_copy_bynumber(parse_regex.match, 3, (PCRE2_UCHAR8 *)type, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 3, (PCRE2_UCHAR8 *)type, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
@@ -243,7 +244,7 @@ static int DetectConfigSetup (DetectEngineCtx *de_ctx, Signature *s, const char
     SCLogDebug("type %s", type);
 
     pcre2len = sizeof(typeval);
-    res = pcre2_substring_copy_bynumber(parse_regex.match, 4, (PCRE2_UCHAR8 *)typeval, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 4, (PCRE2_UCHAR8 *)typeval, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
@@ -256,7 +257,7 @@ static int DetectConfigSetup (DetectEngineCtx *de_ctx, Signature *s, const char
     SCLogDebug("typeval %s", typeval);
 
     pcre2len = sizeof(scope);
-    res = pcre2_substring_copy_bynumber(parse_regex.match, 5, (PCRE2_UCHAR8 *)scope, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 5, (PCRE2_UCHAR8 *)scope, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
@@ -269,7 +270,7 @@ static int DetectConfigSetup (DetectEngineCtx *de_ctx, Signature *s, const char
     SCLogDebug("scope %s", scope);
 
     pcre2len = sizeof(scopeval);
-    res = pcre2_substring_copy_bynumber(parse_regex.match, 6, (PCRE2_UCHAR8 *)scopeval, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 6, (PCRE2_UCHAR8 *)scopeval, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
@@ -299,9 +300,13 @@ static int DetectConfigSetup (DetectEngineCtx *de_ctx, Signature *s, const char
     sm->ctx = (SigMatchCtx*)fd;
     SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_POSTMATCH);
 
+    pcre2_match_data_free(match);
     return 0;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     if (sm != NULL)
         SCFree(sm);
     return -1;
index c63a6d82c89752e57e276b4bbcc2950920c944d7..29c5183dc80f7f2db78de5d040670dfb6e45d58e 100644 (file)
@@ -100,7 +100,7 @@ static int DetectDetectionFilterMatch(
 static DetectThresholdData *DetectDetectionFilterParse(const char *rawstr)
 {
     DetectThresholdData *df = NULL;
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2_len;
     const char *str_ptr = NULL;
     char *args[6] = { NULL, NULL, NULL, NULL, NULL, NULL };
@@ -110,6 +110,7 @@ static DetectThresholdData *DetectDetectionFilterParse(const char *rawstr)
     size_t pos = 0;
     int i = 0;
     char *saveptr = NULL;
+    pcre2_match_data *match = NULL;
 
     copy_str = SCStrdup(rawstr);
     if (unlikely(copy_str == NULL)) {
@@ -132,7 +133,7 @@ static DetectThresholdData *DetectDetectionFilterParse(const char *rawstr)
     if (count_found != 1 || seconds_found != 1 || track_found != 1)
         goto error;
 
-    ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     if (ret < 5) {
         SCLogError("pcre_exec parse error, ret %" PRId32 ", string %s", ret, rawstr);
         goto error;
@@ -147,8 +148,7 @@ static DetectThresholdData *DetectDetectionFilterParse(const char *rawstr)
     df->type = TYPE_DETECTION;
 
     for (i = 0; i < (ret - 1); i++) {
-        res = pcre2_substring_get_bynumber(
-                parse_regex.match, i + 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+        res = pcre2_substring_get_bynumber(match, i + 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
         if (res < 0) {
             SCLogError("pcre2_substring_get_bynumber failed");
             goto error;
@@ -187,6 +187,8 @@ static DetectThresholdData *DetectDetectionFilterParse(const char *rawstr)
         if (args[i] != NULL)
             pcre2_substring_free((PCRE2_UCHAR *)args[i]);
     }
+
+    pcre2_match_data_free(match);
     return df;
 
 error:
@@ -196,6 +198,9 @@ error:
     }
     if (df != NULL)
         SCFree(df);
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     return NULL;
 }
 
index 9943a3b3cc4a5641c08a9a8d921f0101c586160f..82f838446ffaf0549f1abed78e489fa93f64dcc4 100644 (file)
@@ -130,10 +130,11 @@ static DetectEngineEventData *DetectEngineEventParse (const char *rawstr)
 {
     int i;
     DetectEngineEventData *de = NULL;
-    int ret = 0, res = 0, found = 0;
+    int res = 0, found = 0;
     size_t pcre2len;
+    pcre2_match_data *match = NULL;
 
-    ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     if (ret < 1) {
         SCLogError("pcre_exec parse error, ret %" PRId32 ", string %s", ret, rawstr);
         goto error;
@@ -141,7 +142,7 @@ static DetectEngineEventData *DetectEngineEventParse (const char *rawstr)
 
     char copy_str[128] = "";
     pcre2len = sizeof(copy_str);
-    res = pcre2_substring_copy_bynumber(parse_regex.match, 0, (PCRE2_UCHAR8 *)copy_str, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 0, (PCRE2_UCHAR8 *)copy_str, &pcre2len);
 
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
@@ -179,11 +180,15 @@ static DetectEngineEventData *DetectEngineEventParse (const char *rawstr)
         }
     }
 
+    pcre2_match_data_free(match);
     return de;
 
 error:
     if (de)
         SCFree(de);
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     return NULL;
 }
 
index 34f4cf4c69968ac224479094c2b5ed9fac6c4c6a..b82f3274d709807e955c7c938b6c397f928afec1 100644 (file)
@@ -213,10 +213,11 @@ void DetectFastPatternRegister(void)
  */
 static int DetectFastPatternSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
 {
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2len;
     char arg_substr[128] = "";
     DetectContentData *cd = NULL;
+    pcre2_match_data *match = NULL;
 
     SigMatch *pm1 = DetectGetLastSMFromMpmLists(de_ctx, s);
     SigMatch *pm2 = DetectGetLastSMFromLists(s, DETECT_CONTENT, -1);
@@ -278,7 +279,7 @@ static int DetectFastPatternSetup(DetectEngineCtx *de_ctx, Signature *s, const c
     }
 
     /* Execute the regex and populate args with captures. */
-    ret = DetectParsePcreExec(&parse_regex, arg, 0, 0);
+    int ret = DetectParsePcreExec(&parse_regex, &match, arg, 0, 0);
     /* fast pattern only */
     if (ret == 2) {
         if ((cd->flags & DETECT_CONTENT_NEGATED) ||
@@ -298,8 +299,7 @@ static int DetectFastPatternSetup(DetectEngineCtx *de_ctx, Signature *s, const c
         /* fast pattern chop */
     } else if (ret == 4) {
         pcre2len = sizeof(arg_substr);
-        res = pcre2_substring_copy_bynumber(
-                parse_regex.match, 2, (PCRE2_UCHAR8 *)arg_substr, &pcre2len);
+        res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)arg_substr, &pcre2len);
         if (res < 0) {
             SCLogError("pcre2_substring_copy_bynumber failed "
                        "for fast_pattern offset");
@@ -315,8 +315,7 @@ static int DetectFastPatternSetup(DetectEngineCtx *de_ctx, Signature *s, const c
         }
 
         pcre2len = sizeof(arg_substr);
-        res = pcre2_substring_copy_bynumber(
-                parse_regex.match, 3, (PCRE2_UCHAR8 *)arg_substr, &pcre2len);
+        res = pcre2_substring_copy_bynumber(match, 3, (PCRE2_UCHAR8 *)arg_substr, &pcre2len);
         if (res < 0) {
             SCLogError("pcre2_substring_copy_bynumber failed "
                        "for fast_pattern offset");
@@ -356,9 +355,13 @@ static int DetectFastPatternSetup(DetectEngineCtx *de_ctx, Signature *s, const c
 
     cd->flags |= DETECT_CONTENT_FAST_PATTERN;
 
+    pcre2_match_data_free(match);
     return 0;
 
  error:
+     if (match) {
+         pcre2_match_data_free(match);
+     }
     return -1;
 }
 
index d19763d5eeb38b7a4f0359b7d27372d6875fbd48..c53a93d78dd27317b278688f2a3ee5e653feef98 100644 (file)
@@ -351,8 +351,9 @@ static int DetectFilestoreSetup (DetectEngineCtx *de_ctx, Signature *s, const ch
     DetectFilestoreData *fd = NULL;
     SigMatch *sm = NULL;
     char *args[3] = {NULL,NULL,NULL};
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2len;
+    pcre2_match_data *match = NULL;
 
     /* filestore and bypass keywords can't work together */
     if (s->flags & SIG_FLAG_BYPASS) {
@@ -372,7 +373,7 @@ static int DetectFilestoreSetup (DetectEngineCtx *de_ctx, Signature *s, const ch
         char str_2[32];
         SCLogDebug("str %s", str);
 
-        ret = DetectParsePcreExec(&parse_regex, str, 0, 0);
+        int ret = DetectParsePcreExec(&parse_regex, &match, str, 0, 0);
         if (ret < 1 || ret > 4) {
             SCLogError("parse error, ret %" PRId32 ", string %s", ret, str);
             goto error;
@@ -380,8 +381,7 @@ static int DetectFilestoreSetup (DetectEngineCtx *de_ctx, Signature *s, const ch
 
         if (ret > 1) {
             pcre2len = sizeof(str_0);
-            res = pcre2_substring_copy_bynumber(
-                    parse_regex.match, 1, (PCRE2_UCHAR8 *)str_0, &pcre2len);
+            res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)str_0, &pcre2len);
             if (res < 0) {
                 SCLogError("pcre2_substring_copy_bynumber failed");
                 goto error;
@@ -390,8 +390,7 @@ static int DetectFilestoreSetup (DetectEngineCtx *de_ctx, Signature *s, const ch
 
             if (ret > 2) {
                 pcre2len = sizeof(str_1);
-                res = pcre2_substring_copy_bynumber(
-                        parse_regex.match, 2, (PCRE2_UCHAR8 *)str_1, &pcre2len);
+                res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)str_1, &pcre2len);
                 if (res < 0) {
                     SCLogError("pcre2_substring_copy_bynumber failed");
                     goto error;
@@ -400,8 +399,7 @@ static int DetectFilestoreSetup (DetectEngineCtx *de_ctx, Signature *s, const ch
             }
             if (ret > 3) {
                 pcre2len = sizeof(str_2);
-                res = pcre2_substring_copy_bynumber(
-                        parse_regex.match, 3, (PCRE2_UCHAR8 *)str_2, &pcre2len);
+                res = pcre2_substring_copy_bynumber(match, 3, (PCRE2_UCHAR8 *)str_2, &pcre2len);
                 if (res < 0) {
                     SCLogError("pcre2_substring_copy_bynumber failed");
                     goto error;
@@ -477,11 +475,17 @@ static int DetectFilestoreSetup (DetectEngineCtx *de_ctx, Signature *s, const ch
     sm->ctx = NULL;
     SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_POSTMATCH);
 
-
     s->flags |= SIG_FLAG_FILESTORE;
+
+    if (match)
+        pcre2_match_data_free(match);
+
     return 0;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     if (sm != NULL)
         SCFree(sm);
     return -1;
index 4739fdd48ac5b01160c181b25b0803de2a33c683..09787515722c4e1aceea07beea7ca6e63bb5243f 100644 (file)
@@ -173,11 +173,12 @@ static DetectFlowData *DetectFlowParse (DetectEngineCtx *de_ctx, const char *flo
 {
     DetectFlowData *fd = NULL;
     char *args[3] = {NULL,NULL,NULL};
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2len;
     char str1[16] = "", str2[16] = "", str3[16] = "";
+    pcre2_match_data *match = NULL;
 
-    ret = DetectParsePcreExec(&parse_regex, flowstr, 0, 0);
+    int ret = DetectParsePcreExec(&parse_regex, &match, flowstr, 0, 0);
     if (ret < 1 || ret > 4) {
         SCLogError("parse error, ret %" PRId32 ", string %s", ret, flowstr);
         goto error;
@@ -185,7 +186,7 @@ static DetectFlowData *DetectFlowParse (DetectEngineCtx *de_ctx, const char *flo
 
     if (ret > 1) {
         pcre2len = sizeof(str1);
-        res = SC_Pcre2SubstringCopy(parse_regex.match, 1, (PCRE2_UCHAR8 *)str1, &pcre2len);
+        res = SC_Pcre2SubstringCopy(match, 1, (PCRE2_UCHAR8 *)str1, &pcre2len);
         if (res < 0) {
             SCLogError("pcre2_substring_copy_bynumber failed");
             goto error;
@@ -194,8 +195,7 @@ static DetectFlowData *DetectFlowParse (DetectEngineCtx *de_ctx, const char *flo
 
         if (ret > 2) {
             pcre2len = sizeof(str2);
-            res = pcre2_substring_copy_bynumber(
-                    parse_regex.match, 2, (PCRE2_UCHAR8 *)str2, &pcre2len);
+            res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)str2, &pcre2len);
             if (res < 0) {
                 SCLogError("pcre2_substring_copy_bynumber failed");
                 goto error;
@@ -204,8 +204,7 @@ static DetectFlowData *DetectFlowParse (DetectEngineCtx *de_ctx, const char *flo
         }
         if (ret > 3) {
             pcre2len = sizeof(str3);
-            res = pcre2_substring_copy_bynumber(
-                    parse_regex.match, 3, (PCRE2_UCHAR8 *)str3, &pcre2len);
+            res = pcre2_substring_copy_bynumber(match, 3, (PCRE2_UCHAR8 *)str3, &pcre2len);
             if (res < 0) {
                 SCLogError("pcre2_substring_copy_bynumber failed");
                 goto error;
@@ -318,9 +317,13 @@ static DetectFlowData *DetectFlowParse (DetectEngineCtx *de_ctx, const char *flo
             //printf("args[%" PRId32 "]: %s match_cnt: %" PRId32 " flags: 0x%02X\n", i, args[i], fd->match_cnt, fd->flags);
         }
     }
+    pcre2_match_data_free(match);
     return fd;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     if (fd != NULL)
         DetectFlowFree(de_ctx, fd);
     return NULL;
index a0ccda9cf10dab3e6216235a416614535992c720..ad9096b4ae9e8cfdc9aa69d032505bb67601f70d 100644 (file)
@@ -222,28 +222,29 @@ int DetectFlowbitMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
 static int DetectFlowbitParse(const char *str, char *cmd, int cmd_len, char *name,
     int name_len)
 {
-    int count, rc;
+    int rc;
     size_t pcre2len;
+    pcre2_match_data *match = NULL;
 
-    count = DetectParsePcreExec(&parse_regex, str, 0, 0);
+    int count = DetectParsePcreExec(&parse_regex, &match, str, 0, 0);
     if (count != 2 && count != 3) {
         SCLogError("\"%s\" is not a valid setting for flowbits.", str);
-        return 0;
+        goto error;
     }
 
     pcre2len = cmd_len;
-    rc = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)cmd, &pcre2len);
+    rc = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)cmd, &pcre2len);
     if (rc < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
-        return 0;
+        goto error;
     }
 
     if (count == 3) {
         pcre2len = name_len;
-        rc = pcre2_substring_copy_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 *)name, &pcre2len);
+        rc = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)name, &pcre2len);
         if (rc < 0) {
             SCLogError("pcre2_substring_copy_bynumber failed");
-            return 0;
+            goto error;
         }
 
         /* Trim trailing whitespace. */
@@ -256,13 +257,20 @@ static int DetectFlowbitParse(const char *str, char *cmd, int cmd_len, char *nam
             for (size_t i = 0; i < strlen(name); i++) {
                 if (isblank(name[i])) {
                     SCLogError("spaces not allowed in flowbit names");
-                    return 0;
+                    goto error;
                 }
             }
         }
     }
 
+    pcre2_match_data_free(match);
     return 1;
+
+error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
+    return 0;
 }
 
 int DetectFlowbitSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
index 7b9c22385587f8d39befd6a02d56764fc5b7e109..facf1c8ad7d8f2facdbde71f1f2b50a8af8fea85 100644 (file)
@@ -230,27 +230,28 @@ static DetectFlowintData *DetectFlowintParse(DetectEngineCtx *de_ctx, const char
     char *varname = NULL;
     char *varval = NULL;
     char *modstr = NULL;
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2_len;
     uint8_t modifier = FLOWINT_MODIFIER_UNKNOWN;
     unsigned long long value_long = 0;
     const char *str_ptr;
+    pcre2_match_data *match = NULL;
 
-    ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     if (ret < 3 || ret > 4) {
         SCLogError("\"%s\" is not a valid setting for flowint(ret = %d).", rawstr, ret);
-        return NULL;
+        goto error;
     }
 
     /* Get our flowint varname */
-    res = pcre2_substring_get_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+    res = pcre2_substring_get_bynumber(match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
     if (res < 0 || str_ptr == NULL) {
         SCLogError("pcre2_substring_get_bynumber failed");
         goto error;
     }
     varname = (char *)str_ptr;
 
-    res = pcre2_substring_get_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+    res = pcre2_substring_get_bynumber(match, 2, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
     if (res < 0 || str_ptr == NULL) {
         SCLogError("pcre2_substring_get_bynumber failed");
         goto error;
@@ -296,8 +297,7 @@ static DetectFlowintData *DetectFlowintParse(DetectEngineCtx *de_ctx, const char
         if (ret < 4)
             goto error;
 
-        res = pcre2_substring_get_bynumber(
-                parse_regex.match, 3, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+        res = pcre2_substring_get_bynumber(match, 3, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
         varval = (char *)str_ptr;
         if (res < 0 || varval == NULL || strcmp(varval, "") == 0) {
             SCLogError("pcre2_substring_get_bynumber failed");
@@ -339,8 +339,12 @@ static DetectFlowintData *DetectFlowintParse(DetectEngineCtx *de_ctx, const char
     pcre2_substring_free((PCRE2_UCHAR *)modstr);
     if (varval)
         pcre2_substring_free((PCRE2_UCHAR *)varval);
+    pcre2_match_data_free(match);
     return sfd;
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     if (varname)
         pcre2_substring_free((PCRE2_UCHAR *)varname);
     if (varval)
index c8cffeccede499a9f0599afc81ccc426de20f745..6df7f38c772da7203ce7b88b47984ad5797c01ab 100644 (file)
@@ -116,28 +116,33 @@ static int DetectFlowvarSetup (DetectEngineCtx *de_ctx, Signature *s, const char
     DetectFlowvarData *fd = NULL;
     SigMatch *sm = NULL;
     char varname[64], varcontent[64];
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2len;
     uint8_t *content = NULL;
     uint16_t contentlen = 0;
     uint32_t contentflags = s->init_data->negated ? DETECT_CONTENT_NEGATED : 0;
+    pcre2_match_data *match = NULL;
 
-    ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     if (ret != 3) {
         SCLogError("\"%s\" is not a valid setting for flowvar.", rawstr);
+        if (match) {
+            pcre2_match_data_free(match);
+        }
         return -1;
     }
 
     pcre2len = sizeof(varname);
-    res = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)varname, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)varname, &pcre2len);
     if (res < 0) {
+        pcre2_match_data_free(match);
         SCLogError("pcre2_substring_copy_bynumber failed");
         return -1;
     }
 
     pcre2len = sizeof(varcontent);
-    res = pcre2_substring_copy_bynumber(
-            parse_regex.match, 2, (PCRE2_UCHAR8 *)varcontent, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)varcontent, &pcre2len);
+    pcre2_match_data_free(match);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         return -1;
index 421dfe5b2b1d589a6c7f66dd5abecc20e877b72d..0c266557864bbd5a2e19c39e39ee730997272668 100644 (file)
@@ -169,21 +169,22 @@ static int DetectFragBitsMatch (DetectEngineThreadCtx *det_ctx,
 static DetectFragBitsData *DetectFragBitsParse (const char *rawstr)
 {
     DetectFragBitsData *de = NULL;
-    int ret = 0, found = 0, res = 0;
+    int found = 0, res = 0;
     size_t pcre2_len;
     const char *str_ptr = NULL;
     char *args[2] = { NULL, NULL};
     char *ptr;
     int i;
+    pcre2_match_data *match = NULL;
 
-    ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     if (ret < 1) {
         SCLogError("pcre_exec parse error, ret %" PRId32 ", string %s", ret, rawstr);
         goto error;
     }
 
     for (i = 0; i < (ret - 1); i++) {
-        res = SC_Pcre2SubstringGet(parse_regex.match, i + 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+        res = SC_Pcre2SubstringGet(match, i + 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
         if (res < 0) {
             SCLogError("pcre2_substring_get_bynumber failed %d", res);
             goto error;
@@ -255,9 +256,13 @@ static DetectFragBitsData *DetectFragBitsParse (const char *rawstr)
         if (args[i] != NULL)
             pcre2_substring_free((PCRE2_UCHAR8 *)args[i]);
     }
+    pcre2_match_data_free(match);
     return de;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     for (i = 0; i < 2; i++) {
         if (args[i] != NULL)
             pcre2_substring_free((PCRE2_UCHAR8 *)args[i]);
index 0b2a8d2d5b069473c70412e35bd7566edc1c9397..f32f06005a496ae935a577f93262973e963c239d 100644 (file)
@@ -143,20 +143,21 @@ static DetectFragOffsetData *DetectFragOffsetParse (DetectEngineCtx *de_ctx, con
 {
     DetectFragOffsetData *fragoff = NULL;
     char *substr[3] = {NULL, NULL, NULL};
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2_len;
     int i;
     const char *str_ptr;
     char *mode = NULL;
+    pcre2_match_data *match = NULL;
 
-    ret = DetectParsePcreExec(&parse_regex, fragoffsetstr, 0, 0);
+    int ret = DetectParsePcreExec(&parse_regex, &match, fragoffsetstr, 0, 0);
     if (ret < 1 || ret > 4) {
         SCLogError("Parse error %s", fragoffsetstr);
         goto error;
     }
 
     for (i = 1; i < ret; i++) {
-        res = SC_Pcre2SubstringGet(parse_regex.match, i, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+        res = SC_Pcre2SubstringGet(match, i, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
         if (res < 0) {
             SCLogError("pcre2_substring_get_bynumber failed");
             goto error;
@@ -200,9 +201,13 @@ static DetectFragOffsetData *DetectFragOffsetParse (DetectEngineCtx *de_ctx, con
             pcre2_substring_free((PCRE2_UCHAR8 *)substr[i]);
     }
 
+    pcre2_match_data_free(match);
     return fragoff;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     for (i = 0; i < 3; i++) {
         if (substr[i] != NULL)
             pcre2_substring_free((PCRE2_UCHAR8 *)substr[i]);
index 0bd22d14b2ad9f67ea968d677a7d629b205c04a6..c07847dff3f8cc87eb50bcfaa61ec3c58797dabc 100644 (file)
@@ -131,15 +131,16 @@ static DetectFtpdataData *DetectFtpdataParse(const char *ftpcommandstr)
     DetectFtpdataData *ftpcommandd = NULL;
     char arg1[5] = "";
     size_t pcre2len;
+    pcre2_match_data *match = NULL;
 
-    int ret = DetectParsePcreExec(&parse_regex, ftpcommandstr, 0, 0);
+    int ret = DetectParsePcreExec(&parse_regex, &match, ftpcommandstr, 0, 0);
     if (ret != 2) {
         SCLogError("parse error, ret %" PRId32 "", ret);
         goto error;
     }
 
     pcre2len = sizeof(arg1);
-    int res = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)arg1, &pcre2len);
+    int res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)arg1, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
@@ -158,9 +159,13 @@ static DetectFtpdataData *DetectFtpdataParse(const char *ftpcommandstr)
         goto error;
     }
 
+    pcre2_match_data_free(match);
     return ftpcommandd;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     if (ftpcommandd)
         SCFree(ftpcommandd);
     return NULL;
index ee8203e18aec83a0884f24b096f94e09b079eda8..8297b2f8ae51101285f5ce4290b54606e02a4aa0 100644 (file)
@@ -284,41 +284,48 @@ static int DetectHostbitMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
 static int DetectHostbitParse(const char *str, char *cmd, int cmd_len,
     char *name, int name_len, char *dir, int dir_len)
 {
-    int count, rc;
+    int rc;
     size_t pcre2len;
 
-    count = DetectParsePcreExec(&parse_regex, str, 0, 0);
+    pcre2_match_data *match = NULL;
+    int count = DetectParsePcreExec(&parse_regex, &match, str, 0, 0);
     if (count != 2 && count != 3 && count != 4) {
         SCLogError("\"%s\" is not a valid setting for hostbits.", str);
-        return 0;
+        goto error;
     }
 
     pcre2len = cmd_len;
-    rc = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)cmd, &pcre2len);
+    rc = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)cmd, &pcre2len);
     if (rc < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
-        return 0;
+        goto error;
     }
 
     if (count >= 3) {
         pcre2len = name_len;
-        rc = pcre2_substring_copy_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 *)name, &pcre2len);
+        rc = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)name, &pcre2len);
         if (rc < 0) {
             SCLogError("pcre2_substring_copy_bynumber failed");
-            return 0;
+            goto error;
         }
         if (count >= 4) {
             pcre2len = dir_len;
-            rc = pcre2_substring_copy_bynumber(
-                    parse_regex.match, 3, (PCRE2_UCHAR8 *)dir, &pcre2len);
+            rc = pcre2_substring_copy_bynumber(match, 3, (PCRE2_UCHAR8 *)dir, &pcre2len);
             if (rc < 0) {
                 SCLogError("pcre2_substring_copy_bynumber failed");
-                return 0;
+                goto error;
             }
         }
     }
 
+    pcre2_match_data_free(match);
     return 1;
+
+error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
+    return 0;
 }
 
 int DetectHostbitSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
index 713ca33f3cd36e82b3b15470868260f6586622b7..aee14bc377e9e0fd6669e739f5c98c643efc6b72 100644 (file)
@@ -161,10 +161,11 @@ static DetectIcmpIdData *DetectIcmpIdParse (DetectEngineCtx *de_ctx, const char
 {
     DetectIcmpIdData *iid = NULL;
     char *substr[3] = {NULL, NULL, NULL};
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2_len;
 
-    ret = DetectParsePcreExec(&parse_regex, icmpidstr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, icmpidstr, 0, 0);
     if (ret < 1 || ret > 4) {
         SCLogError("Parse error %s", icmpidstr);
         goto error;
@@ -173,7 +174,7 @@ static DetectIcmpIdData *DetectIcmpIdParse (DetectEngineCtx *de_ctx, const char
     int i;
     const char *str_ptr;
     for (i = 1; i < ret; i++) {
-        res = SC_Pcre2SubstringGet(parse_regex.match, i, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+        res = SC_Pcre2SubstringGet(match, i, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
         if (res < 0) {
             SCLogError("pcre2_substring_get_bynumber failed");
             goto error;
@@ -211,9 +212,13 @@ static DetectIcmpIdData *DetectIcmpIdParse (DetectEngineCtx *de_ctx, const char
         if (substr[i] != NULL)
             pcre2_substring_free((PCRE2_UCHAR8 *)substr[i]);
     }
+    pcre2_match_data_free(match);
     return iid;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     for (i = 0; i < 3; i++) {
         if (substr[i] != NULL)
             pcre2_substring_free((PCRE2_UCHAR8 *)substr[i]);
index 345f5d0129077c76040aee4b6f87466f67e3378a..18a53fa68c264a788ed334ef6705983097e6b925 100644 (file)
@@ -162,19 +162,20 @@ static DetectIcmpSeqData *DetectIcmpSeqParse (DetectEngineCtx *de_ctx, const cha
 {
     DetectIcmpSeqData *iseq = NULL;
     char *substr[3] = {NULL, NULL, NULL};
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2_len;
     int i;
     const char *str_ptr;
 
-    ret = DetectParsePcreExec(&parse_regex, icmpseqstr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, icmpseqstr, 0, 0);
     if (ret < 1 || ret > 4) {
         SCLogError("Parse error %s", icmpseqstr);
         goto error;
     }
 
     for (i = 1; i < ret; i++) {
-        res = SC_Pcre2SubstringGet(parse_regex.match, i, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+        res = SC_Pcre2SubstringGet(match, i, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
         if (res < 0) {
             SCLogError("pcre2_substring_get_bynumber failed");
             goto error;
@@ -214,9 +215,13 @@ static DetectIcmpSeqData *DetectIcmpSeqParse (DetectEngineCtx *de_ctx, const cha
             pcre2_substring_free((PCRE2_UCHAR8 *)substr[i]);
     }
 
+    pcre2_match_data_free(match);
     return iseq;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     for (i = 0; i < 3; i++) {
         if (substr[i] != NULL)
             pcre2_substring_free((PCRE2_UCHAR8 *)substr[i]);
index dd1717db7192750cdbd265349e7987ac4cee18e9..52392885a55479504d2c5bb3cbb112a70d397c30 100644 (file)
@@ -123,25 +123,26 @@ static DetectIdData *DetectIdParse (const char *idstr)
 {
     uint16_t temp;
     DetectIdData *id_d = NULL;
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2len;
+    pcre2_match_data *match = NULL;
 
-    ret = DetectParsePcreExec(&parse_regex, idstr, 0, 0);
+    int ret = DetectParsePcreExec(&parse_regex, &match, idstr, 0, 0);
 
     if (ret < 1 || ret > 3) {
         SCLogError("invalid id option '%s'. The id option "
                    "value must be in the range %u - %u",
                 idstr, DETECT_IPID_MIN, DETECT_IPID_MAX);
-        return NULL;
+        goto error;
     }
 
     char copy_str[128] = "";
     char *tmp_str;
     pcre2len = sizeof(copy_str);
-    res = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)copy_str, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)copy_str, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
-        return NULL;
+        goto error;
     }
     tmp_str = copy_str;
 
@@ -155,18 +156,25 @@ static DetectIdData *DetectIdParse (const char *idstr)
     /* ok, fill the id data */
     if (StringParseUint16(&temp, 10, 0, (const char *)tmp_str) < 0) {
         SCLogError("invalid id option '%s'", tmp_str);
-        return NULL;
+        goto error;
     }
 
     /* We have a correct id option */
     id_d = SCMalloc(sizeof(DetectIdData));
     if (unlikely(id_d == NULL))
-        return NULL;
+        goto error;
 
     id_d->id = temp;
 
     SCLogDebug("detect-id: will look for ip_id: %u\n", id_d->id);
+    pcre2_match_data_free(match);
     return id_d;
+
+error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
+    return NULL;
 }
 
 /**
index b0454c3d126a5b1c140f337782354d9958b5c6cc..59d245de7611aff712a149ab4d1adc7a0f26645d 100644 (file)
@@ -130,12 +130,13 @@ static DetectIkeChosenSaData *DetectIkeChosenSaParse(const char *rawstr)
      * ike.chosen_sa_attribute:"hash_algorithm=8"
      */
     DetectIkeChosenSaData *dd = NULL;
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2len;
     char attribute[100];
     char value[100];
 
-    ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     if (ret < 3 || ret > 5) {
         SCLogError(
                 "pcre match for ike.chosen_sa_attribute failed, should be: <sa_attribute>=<type>, "
@@ -145,14 +146,14 @@ static DetectIkeChosenSaData *DetectIkeChosenSaParse(const char *rawstr)
     }
 
     pcre2len = sizeof(attribute);
-    res = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)attribute, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)attribute, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
     }
 
     pcre2len = sizeof(value);
-    res = pcre2_substring_copy_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 *)value, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)value, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
@@ -172,9 +173,13 @@ static DetectIkeChosenSaData *DetectIkeChosenSaParse(const char *rawstr)
         goto error;
     }
 
+    pcre2_match_data_free(match);
     return dd;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     if (dd) {
         if (dd->sa_type != NULL)
             SCFree(dd->sa_type);
index ae6a6ed3ab086cf9e5eb2c55c8a54a10f388b498..07e6b7eac9b24e0fae7d62f81c79981433d1023f 100644 (file)
@@ -160,9 +160,10 @@ static DetectIpOptsData *DetectIpOptsParse (const char *rawstr)
 {
     int i;
     DetectIpOptsData *de = NULL;
-    int ret = 0, found = 0;
+    int found = 0;
 
-    ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     if (ret < 1) {
         SCLogError("pcre_exec parse error, ret %" PRId32 ", string %s", ret, rawstr);
         goto error;
@@ -184,9 +185,13 @@ static DetectIpOptsData *DetectIpOptsParse (const char *rawstr)
 
     de->ipopt = ipopts[i].code;
 
+    pcre2_match_data_free(match);
     return de;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     if (de) SCFree(de);
     return NULL;
 }
index 04582384a055e8664fcd616d5dcb80115bafec6b..51aac4f173bc6fda69eae69ac775971bf0370e15 100644 (file)
@@ -85,13 +85,14 @@ static DetectIPProtoData *DetectIPProtoParse(const char *optstr)
 {
     DetectIPProtoData *data = NULL;
     char *args[2] = { NULL, NULL };
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2_len;
     int i;
     const char *str_ptr;
 
     /* Execute the regex and populate args with captures. */
-    ret = DetectParsePcreExec(&parse_regex, optstr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, optstr, 0, 0);
     if (ret != 3) {
         SCLogError("pcre_exec parse error, ret"
                    "%" PRId32 ", string %s",
@@ -100,8 +101,7 @@ static DetectIPProtoData *DetectIPProtoParse(const char *optstr)
     }
 
     for (i = 0; i < (ret - 1); i++) {
-        res = pcre2_substring_get_bynumber(
-                parse_regex.match, i + 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+        res = pcre2_substring_get_bynumber(match, i + 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
         if (res < 0) {
             SCLogError("pcre2_substring_get_bynumber failed");
             goto error;
@@ -142,9 +142,13 @@ static DetectIPProtoData *DetectIPProtoParse(const char *optstr)
             pcre2_substring_free((PCRE2_UCHAR8 *)args[i]);
     }
 
+    pcre2_match_data_free(match);
     return data;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     for (i = 0; i < (ret - 1) && i < 2; i++){
         if (args[i] != NULL)
             pcre2_substring_free((PCRE2_UCHAR8 *)args[i]);
index a032b693682cf2891afe2ae26b2be76d757bd7bf..e0858f1354a4fb102fee5cbb3ed59cd071c16495 100644 (file)
@@ -98,11 +98,12 @@ static DetectIsdataatData *DetectIsdataatParse (DetectEngineCtx *de_ctx, const c
 {
     DetectIsdataatData *idad = NULL;
     char *args[3] = {NULL,NULL,NULL};
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2_len;
     int i=0;
 
-    ret = DetectParsePcreExec(&parse_regex, isdataatstr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, isdataatstr, 0, 0);
     if (ret < 1 || ret > 4) {
         SCLogError("pcre_exec parse error, ret %" PRId32 ", string %s", ret, isdataatstr);
         goto error;
@@ -110,8 +111,7 @@ static DetectIsdataatData *DetectIsdataatParse (DetectEngineCtx *de_ctx, const c
 
     if (ret > 1) {
         const char *str_ptr;
-        res = pcre2_substring_get_bynumber(
-                parse_regex.match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+        res = pcre2_substring_get_bynumber(match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
         if (res < 0) {
             SCLogError("pcre2_substring_get_bynumber failed");
             goto error;
@@ -120,8 +120,7 @@ static DetectIsdataatData *DetectIsdataatParse (DetectEngineCtx *de_ctx, const c
 
 
         if (ret > 2) {
-            res = pcre2_substring_get_bynumber(
-                    parse_regex.match, 2, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+            res = pcre2_substring_get_bynumber(match, 2, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
             if (res < 0) {
                 SCLogError("pcre2_substring_get_bynumber failed");
                 goto error;
@@ -129,8 +128,7 @@ static DetectIsdataatData *DetectIsdataatParse (DetectEngineCtx *de_ctx, const c
             args[1] = (char *)str_ptr;
         }
         if (ret > 3) {
-            res = pcre2_substring_get_bynumber(
-                    parse_regex.match, 3, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+            res = pcre2_substring_get_bynumber(match, 3, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
             if (res < 0) {
                 SCLogError("pcre2_substring_get_bynumber failed");
                 goto error;
@@ -181,11 +179,15 @@ static DetectIsdataatData *DetectIsdataatParse (DetectEngineCtx *de_ctx, const c
                 pcre2_substring_free((PCRE2_UCHAR8 *)args[i]);
         }
 
+        pcre2_match_data_free(match);
         return idad;
 
     }
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     for (i = 0; i < (ret -1) && i < 3; i++){
         if (args[i] != NULL)
             pcre2_substring_free((PCRE2_UCHAR8 *)args[i]);
index ff5c2d48beab3c1e54355e97756aaf06cb48b43f..30c516f8d273ca4659e0fd4822990746a1dff88b 100644 (file)
@@ -126,17 +126,18 @@ static DetectKrb5ErrCodeData *DetectKrb5ErrCodeParse (const char *krb5str)
 {
     DetectKrb5ErrCodeData *krb5d = NULL;
     char arg1[4] = "";
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2len;
 
-    ret = DetectParsePcreExec(&parse_regex, krb5str, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, krb5str, 0, 0);
     if (ret != 2) {
         SCLogError("parse error, ret %" PRId32 "", ret);
         goto error;
     }
 
     pcre2len = sizeof(arg1);
-    res = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)arg1, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)arg1, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
@@ -149,9 +150,13 @@ static DetectKrb5ErrCodeData *DetectKrb5ErrCodeParse (const char *krb5str)
                          (const char *)arg1) < 0) {
         goto error;
     }
+    pcre2_match_data_free(match);
     return krb5d;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     if (krb5d)
         SCFree(krb5d);
     return NULL;
index 12e3eeeb5b303c274f27b9665f65a403b3e0b202..0dd800d6be5840b1894cc924a2596dcd2218d501 100644 (file)
@@ -123,17 +123,18 @@ static DetectKrb5MsgTypeData *DetectKrb5MsgTypeParse (const char *krb5str)
 {
     DetectKrb5MsgTypeData *krb5d = NULL;
     char arg1[4] = "";
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2len;
 
-    ret = DetectParsePcreExec(&parse_regex, krb5str, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, krb5str, 0, 0);
     if (ret != 2) {
         SCLogError("parse error, ret %" PRId32 "", ret);
         goto error;
     }
 
     pcre2len = sizeof(arg1);
-    res = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)arg1, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)arg1, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
@@ -146,9 +147,13 @@ static DetectKrb5MsgTypeData *DetectKrb5MsgTypeParse (const char *krb5str)
                          (const char *)arg1) < 0) {
         goto error;
     }
+    pcre2_match_data_free(match);
     return krb5d;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     if (krb5d)
         SCFree(krb5d);
     return NULL;
index d12bc48ad74012ceab1d5435b061b628c1a21934..b6a46a2a5413b727931b35f43f25698e3fe58638 100644 (file)
@@ -77,7 +77,7 @@ void DetectMarkRegister (void)
  */
 static void * DetectMarkParse (const char *rawstr)
 {
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2_len;
     const char *str_ptr = NULL;
     char *ptr = NULL;
@@ -86,45 +86,47 @@ static void * DetectMarkParse (const char *rawstr)
     uint32_t mask;
     DetectMarkData *data;
 
-    ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     if (ret < 1) {
         SCLogError("pcre_exec parse error, ret %" PRId32 ", string %s", ret, rawstr);
+        pcre2_match_data_free(match);
         return NULL;
     }
 
-    res = pcre2_substring_get_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+    res = pcre2_substring_get_bynumber(match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
     if (res < 0) {
         SCLogError("pcre2_substring_get_bynumber failed");
-        return NULL;
+        goto error;
     }
 
     ptr = (char *)str_ptr;
 
     if (ptr == NULL)
-        return NULL;
+        goto error;
 
     errno = 0;
     mark = strtoul(ptr, &endptr, 0);
     if (errno == ERANGE) {
         SCLogError("Numeric value out of range");
         pcre2_substring_free((PCRE2_UCHAR8 *)ptr);
-        return NULL;
+        goto error;
     }     /* If there is no numeric value in the given string then strtoull(), makes
              endptr equals to ptr and return 0 as result */
     else if (endptr == ptr && mark == 0) {
         SCLogError("No numeric value");
         pcre2_substring_free((PCRE2_UCHAR8 *)ptr);
-        return NULL;
+        goto error;
     } else if (endptr == ptr) {
         SCLogError("Invalid numeric value");
         pcre2_substring_free((PCRE2_UCHAR8 *)ptr);
-        return NULL;
+        goto error;
     }
 
-    res = pcre2_substring_get_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+    res = pcre2_substring_get_bynumber(match, 2, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
     if (res < 0) {
         SCLogError("pcre2_substring_get_bynumber failed");
-        return NULL;
+        goto error;
     }
 
     pcre2_substring_free((PCRE2_UCHAR8 *)ptr);
@@ -133,10 +135,11 @@ static void * DetectMarkParse (const char *rawstr)
     if (ptr == NULL) {
         data = SCMalloc(sizeof(DetectMarkData));
         if (unlikely(data == NULL)) {
-            return NULL;
+            goto error;
         }
         data->mark = mark;
         data->mask = 0xffff;
+        pcre2_match_data_free(match);
         return data;
     }
 
@@ -145,18 +148,18 @@ static void * DetectMarkParse (const char *rawstr)
     if (errno == ERANGE) {
         SCLogError("Numeric value out of range");
         pcre2_substring_free((PCRE2_UCHAR8 *)ptr);
-        return NULL;
+        goto error;
     }     /* If there is no numeric value in the given string then strtoull(), makes
              endptr equals to ptr and return 0 as result */
     else if (endptr == ptr && mask == 0) {
         SCLogError("No numeric value");
         pcre2_substring_free((PCRE2_UCHAR8 *)ptr);
-        return NULL;
+        goto error;
     }
     else if (endptr == ptr) {
         SCLogError("Invalid numeric value");
         pcre2_substring_free((PCRE2_UCHAR8 *)ptr);
-        return NULL;
+        goto error;
     }
 
     SCLogDebug("Rule will set mark 0x%x with mask 0x%x", mark, mask);
@@ -164,11 +167,18 @@ static void * DetectMarkParse (const char *rawstr)
 
     data = SCMalloc(sizeof(DetectMarkData));
     if (unlikely(data == NULL)) {
-        return NULL;
+        goto error;
     }
     data->mark = mark;
     data->mask = mask;
+    pcre2_match_data_free(match);
     return data;
+
+error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
+    return NULL;
 }
 
 #endif /* NFQ */
index d7aa8eb4bda67a6fc2bdaa41a36d79302da4ae3d..49bfae6f4b5244ba7aac78c4ae28cc7dd65b511b 100644 (file)
@@ -116,22 +116,22 @@ static int DetectMQTTConnectFlagsMatch(DetectEngineThreadCtx *det_ctx,
  */
 static DetectMQTTConnectFlagsData *DetectMQTTConnectFlagsParse(const char *rawstr)
 {
-    DetectMQTTConnectFlagsData *de = NULL;
-    int ret = 0;
+    char copy[strlen(rawstr) + 1];
 
-    ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    DetectMQTTConnectFlagsData *de = SCCalloc(1, sizeof(DetectMQTTConnectFlagsData));
+    if (unlikely(de == NULL))
+        return NULL;
+
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     if (ret < 1) {
         SCLogError("invalid flag definition: %s", rawstr);
-        return NULL;
+        goto error;
     }
 
-    de = SCCalloc(1, sizeof(DetectMQTTConnectFlagsData));
-    if (unlikely(de == NULL))
-        return NULL;
     de->username = de->password = de->will = MQTT_DONT_CARE;
     de->will_retain = de->clean_session = MQTT_DONT_CARE;
 
-    char copy[strlen(rawstr)+1];
     strlcpy(copy, rawstr, sizeof(copy));
     char *xsaveptr = NULL;
     char *flagv = strtok_r(copy, ",", &xsaveptr);
@@ -188,11 +188,15 @@ static DetectMQTTConnectFlagsData *DetectMQTTConnectFlagsParse(const char *rawst
         flagv = strtok_r(NULL, ",", &xsaveptr);
     }
 
+    pcre2_match_data_free(match);
     return de;
 
 error:
-    /* de can't be NULL here */
-    SCFree(de);
+    if (match) {
+        pcre2_match_data_free(match);
+    }
+    if (de)
+        SCFree(de);
     return NULL;
 }
 
index a77ebff19070c6ff8aa5c617de085640281224f0..799e1668e4043c25eff1982d834d079cb21330e5 100644 (file)
@@ -111,18 +111,22 @@ static int DetectMQTTFlagsMatch(DetectEngineThreadCtx *det_ctx,
  */
 static DetectMQTTFlagsData *DetectMQTTFlagsParse(const char *rawstr)
 {
-    DetectMQTTFlagsData *de = NULL;
-    int ret = 0;
 
-    ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    DetectMQTTFlagsData *de = SCCalloc(1, sizeof(DetectMQTTFlagsData));
+    if (unlikely(de == NULL))
+        return NULL;
+
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     if (ret < 1) {
         SCLogError("invalid flag definition: %s", rawstr);
+        if (match) {
+            pcre2_match_data_free(match);
+        }
+        SCFree(de);
         return NULL;
     }
 
-    de = SCCalloc(1, sizeof(DetectMQTTFlagsData));
-    if (unlikely(de == NULL))
-        return NULL;
     de->retain = de->dup = MQTT_DONT_CARE;
 
     char copy[strlen(rawstr)+1];
@@ -168,11 +172,15 @@ static DetectMQTTFlagsData *DetectMQTTFlagsParse(const char *rawstr)
         flagv = strtok_r(NULL, ",", &xsaveptr);
     }
 
+    pcre2_match_data_free(match);
     return de;
 
 error:
-    /* de can't be NULL here */
-    SCFree(de);
+    if (match) {
+        pcre2_match_data_free(match);
+    }
+    if (de)
+        SCFree(de);
     return NULL;
 }
 
index 2e109c3ecb2b7afe373302ff37a660e15260e8a0..33d739300dd8ee9a0d5d0be4518b3746a74ce055 100644 (file)
@@ -2620,11 +2620,14 @@ error:
 
 static DetectParseRegex *g_detect_parse_regex_list = NULL;
 
-int DetectParsePcreExec(
-        DetectParseRegex *parse_regex, const char *str, int start_offset, int options)
+int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str,
+        int start_offset, int options)
 {
-    return pcre2_match(parse_regex->regex, (PCRE2_SPTR8)str, strlen(str), options, start_offset,
-            parse_regex->match, NULL);
+    *match = pcre2_match_data_create_from_pattern(parse_regex->regex, NULL);
+    if (*match)
+        return pcre2_match(parse_regex->regex, (PCRE2_SPTR8)str, strlen(str), options, start_offset,
+                *match, NULL);
+    return -1;
 }
 
 void DetectParseFreeRegex(DetectParseRegex *r)
index 33a2d515f0d28d71516c08018308ac37e8a6cee6..a7f2c4d17df7192bdff6f349088aa0a6a2e52435 100644 (file)
@@ -114,8 +114,8 @@ void DetectParseFreeRegexes(void);
 void DetectParseFreeRegex(DetectParseRegex *r);
 
 /* parse regex exec */
-int DetectParsePcreExec(
-        DetectParseRegex *parse_regex, const char *str, int start_offset, int options);
+int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str,
+        int start_offset, int options);
 int SC_Pcre2SubstringCopy(
         pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR *buffer, PCRE2_SIZE *bufflen);
 int SC_Pcre2SubstringGet(pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR **bufferptr,
index 4d366430edcbc4ebf1190a9cb4a174762068a171..ec34463259e56dca964f7956788ae09ce9815b11 100644 (file)
@@ -89,30 +89,31 @@ static void DetectPktvarFree(DetectEngineCtx *de_ctx, void *ptr)
 static int DetectPktvarSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
 {
     char *varname = NULL, *varcontent = NULL;
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2_len;
     uint8_t *content = NULL;
     uint16_t len = 0;
 
-    ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     if (ret != 3) {
         SCLogError("\"%s\" is not a valid setting for pktvar.", rawstr);
-        return -1;
+        goto error;
     }
 
     const char *str_ptr;
-    res = pcre2_substring_get_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+    res = pcre2_substring_get_bynumber(match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
     if (res < 0) {
         SCLogError("pcre2_substring_get_bynumber failed");
-        return -1;
+        goto error;
     }
     varname = (char *)str_ptr;
 
-    res = pcre2_substring_get_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+    res = pcre2_substring_get_bynumber(match, 2, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
     if (res < 0) {
         pcre2_substring_free((PCRE2_UCHAR8 *)varname);
         SCLogError("pcre2_substring_get_bynumber failed");
-        return -1;
+        goto error;
     }
     varcontent = (char *)str_ptr;
 
@@ -132,7 +133,7 @@ static int DetectPktvarSetup (DetectEngineCtx *de_ctx, Signature *s, const char
     if (ret == -1 || content == NULL) {
         pcre2_substring_free((PCRE2_UCHAR8 *)varname);
         pcre2_substring_free((PCRE2_UCHAR8 *)varcontent);
-        return -1;
+        goto error;
     }
     pcre2_substring_free((PCRE2_UCHAR8 *)varcontent);
 
@@ -140,7 +141,7 @@ static int DetectPktvarSetup (DetectEngineCtx *de_ctx, Signature *s, const char
     if (unlikely(cd == NULL)) {
         pcre2_substring_free((PCRE2_UCHAR8 *)varname);
         SCFree(content);
-        return -1;
+        goto error;
     }
 
     cd->content = content;
@@ -153,11 +154,19 @@ static int DetectPktvarSetup (DetectEngineCtx *de_ctx, Signature *s, const char
     SigMatch *sm = SigMatchAlloc();
     if (unlikely(sm == NULL)) {
         DetectPktvarFree(de_ctx, cd);
-        return -1;
+        goto error;
     }
     sm->type = DETECT_PKTVAR;
     sm->ctx = (SigMatchCtx *)cd;
 
     SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_MATCH);
+
+    pcre2_match_data_free(match);
     return 0;
+
+error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
+    return -1;
 }
index 688eeed438a8a2485d367a78061f0e449c51823c..81ee72966fb5c5edcb9bf98e1508b3b15367df29 100644 (file)
@@ -61,28 +61,30 @@ void DetectPriorityRegister (void)
 static int DetectPrioritySetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
 {
     char copy_str[128] = "";
-
-    int ret = 0;
     size_t pcre2len;
 
-    ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     if (ret < 0) {
         SCLogError("Invalid Priority in Signature "
                    "- %s",
                 rawstr);
+        if (match)
+            pcre2_match_data_free(match);
         return -1;
     }
 
     pcre2len = sizeof(copy_str);
-    ret = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)copy_str, &pcre2len);
+    ret = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)copy_str, &pcre2len);
     if (ret < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
+        pcre2_match_data_free(match);
         return -1;
     }
 
-    long prio = 0;
+    pcre2_match_data_free(match);
     char *endptr = NULL;
-    prio = strtol(copy_str, &endptr, 10);
+    long prio = strtol(copy_str, &endptr, 10);
     if (endptr == NULL || *endptr != '\0') {
         SCLogError("Saw an invalid character as arg "
                    "to priority keyword");
index d3fe1a05597c31316f74ab7a418d5b79542759b9..aaa723db496320b9c572fca5b9891510207ce6dd 100644 (file)
@@ -95,33 +95,38 @@ static DetectReference *DetectReferenceParse(const char *rawstr, DetectEngineCtx
 {
     SCEnter();
 
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2len;
     char key[REFERENCE_SYSTEM_NAME_MAX] = "";
     char content[REFERENCE_CONTENT_NAME_MAX] = "";
 
-    ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     if (ret < 2) {
         SCLogError("Unable to parse \"reference\" "
                    "keyword argument - \"%s\".   Invalid argument.",
                 rawstr);
+        if (match) {
+            pcre2_match_data_free(match);
+        }
         return NULL;
     }
 
     DetectReference *ref = SCCalloc(1, sizeof(DetectReference));
     if (unlikely(ref == NULL)) {
+        pcre2_match_data_free(match);
         return NULL;
     }
 
     pcre2len = sizeof(key);
-    res = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)key, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)key, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
     }
 
     pcre2len = sizeof(content);
-    res = pcre2_substring_copy_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 *)content, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)content, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
@@ -158,10 +163,14 @@ static DetectReference *DetectReferenceParse(const char *rawstr, DetectEngineCtx
         goto error;
     }
 
+    pcre2_match_data_free(match);
     /* free the substrings */
     SCReturnPtr(ref, "Reference");
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     DetectReferenceFree(ref);
     SCReturnPtr(NULL, "Reference");
 }
index 4983982ad846b647c449f908dd69d7c388f1799d..ff82d98fa690b72301da536d0600175049007c46 100644 (file)
@@ -158,9 +158,10 @@ static DetectRfbSecresultData *DetectRfbSecresultParse (const char *rawstr)
 {
     int i;
     DetectRfbSecresultData *de = NULL;
-    int ret = 0, found = 0;
+    int found = 0;
 
-    ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     if (ret < 1) {
         SCLogError("pcre_exec parse error, ret %" PRId32 ", string %s", ret, rawstr);
         goto error;
@@ -184,9 +185,13 @@ static DetectRfbSecresultData *DetectRfbSecresultParse (const char *rawstr)
 
     de->result = results[i].code;
 
+    pcre2_match_data_free(match);
     return de;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     if (de) SCFree(de);
     return NULL;
 }
index 0fee3fc1d12fec34f4e55a67748db4a32da7ae16..2739d6218cafd5399cae200f78a74bc4c6f988af 100644 (file)
@@ -149,10 +149,11 @@ static DetectRpcData *DetectRpcParse (DetectEngineCtx *de_ctx, const char *rpcst
 {
     DetectRpcData *rd = NULL;
     char *args[3] = {NULL,NULL,NULL};
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2_len;
 
-    ret = DetectParsePcreExec(&parse_regex, rpcstr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, rpcstr, 0, 0);
     if (ret < 1 || ret > 4) {
         SCLogError("parse error, ret %" PRId32 ", string %s", ret, rpcstr);
         goto error;
@@ -160,8 +161,7 @@ static DetectRpcData *DetectRpcParse (DetectEngineCtx *de_ctx, const char *rpcst
 
     if (ret > 1) {
         const char *str_ptr;
-        res = pcre2_substring_get_bynumber(
-                parse_regex.match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+        res = pcre2_substring_get_bynumber(match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
         if (res < 0) {
             SCLogError("pcre2_substring_get_bynumber failed");
             goto error;
@@ -169,8 +169,7 @@ static DetectRpcData *DetectRpcParse (DetectEngineCtx *de_ctx, const char *rpcst
         args[0] = (char *)str_ptr;
 
         if (ret > 2) {
-            res = pcre2_substring_get_bynumber(
-                    parse_regex.match, 2, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+            res = pcre2_substring_get_bynumber(match, 2, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
             if (res < 0) {
                 SCLogError("pcre2_substring_get_bynumber failed");
                 goto error;
@@ -178,8 +177,7 @@ static DetectRpcData *DetectRpcParse (DetectEngineCtx *de_ctx, const char *rpcst
             args[1] = (char *)str_ptr;
         }
         if (ret > 3) {
-            res = pcre2_substring_get_bynumber(
-                    parse_regex.match, 3, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+            res = pcre2_substring_get_bynumber(match, 3, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
             if (res < 0) {
                 SCLogError("pcre2_substring_get_bynumber failed");
                 goto error;
@@ -237,9 +235,13 @@ static DetectRpcData *DetectRpcParse (DetectEngineCtx *de_ctx, const char *rpcst
         if (args[i] != NULL)
             pcre2_substring_free((PCRE2_UCHAR8 *)args[i]);
     }
+    pcre2_match_data_free(match);
     return rd;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     for (i = 0; i < (ret -1) && i < 3; i++){
         if (args[i] != NULL)
             pcre2_substring_free((PCRE2_UCHAR8 *)args[i]);
index 0f5edd58d501167c960457e303160218fe984857..d053c29a792d9bd9284f1f7e6995b3ced73ad32e 100644 (file)
@@ -122,19 +122,20 @@ static int DetectSNMPPduTypeMatch (DetectEngineThreadCtx *det_ctx,
 static DetectSNMPPduTypeData *DetectSNMPPduTypeParse (const char *rawstr)
 {
     DetectSNMPPduTypeData *dd = NULL;
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2len;
     char value1[20] = "";
     char *endptr = NULL;
 
-    ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     if (ret != 2) {
         SCLogError("Parse error %s", rawstr);
         goto error;
     }
 
     pcre2len = sizeof(value1);
-    res = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)value1, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)value1, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
@@ -152,9 +153,13 @@ static DetectSNMPPduTypeData *DetectSNMPPduTypeParse (const char *rawstr)
         goto error;
     }
 
+    pcre2_match_data_free(match);
     return dd;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     if (dd)
         SCFree(dd);
     return NULL;
index 65f779c16532eb56886171c9283a8a6afd7bf976..9115d8affb83ca7f60a588967ad6617fc2eb8cdc 100644 (file)
@@ -160,10 +160,11 @@ static int DetectSshVersionMatch (DetectEngineThreadCtx *det_ctx,
 static DetectSshVersionData *DetectSshVersionParse (DetectEngineCtx *de_ctx, const char *str)
 {
     DetectSshVersionData *ssh = NULL;
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2_len;
 
-    ret = DetectParsePcreExec(&parse_regex, str, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, str, 0, 0);
     if (ret < 1 || ret > 3) {
         SCLogError("invalid ssh.protoversion option");
         goto error;
@@ -171,8 +172,7 @@ static DetectSshVersionData *DetectSshVersionParse (DetectEngineCtx *de_ctx, con
 
     if (ret > 1) {
         const char *str_ptr;
-        res = pcre2_substring_get_bynumber(
-                parse_regex.match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+        res = pcre2_substring_get_bynumber(match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
         if (res < 0) {
             SCLogError("pcre2_substring_get_bynumber failed");
             goto error;
@@ -206,9 +206,13 @@ static DetectSshVersionData *DetectSshVersionParse (DetectEngineCtx *de_ctx, con
         SCLogDebug("will look for ssh %s", ssh->ver);
     }
 
+    pcre2_match_data_free(match);
     return ssh;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     if (ssh != NULL)
         DetectSshVersionFree(de_ctx, ssh);
     return NULL;
index b8a74e538a27de526dbf342928248d93dd6c9ed0..5fec33ac0eef0729e3c3279610f582700a4c6d34 100644 (file)
@@ -156,10 +156,11 @@ static int DetectSshSoftwareVersionMatch (DetectEngineThreadCtx *det_ctx,
 static DetectSshSoftwareVersionData *DetectSshSoftwareVersionParse (DetectEngineCtx *de_ctx, const char *str)
 {
     DetectSshSoftwareVersionData *ssh = NULL;
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2_len;
 
-    ret = DetectParsePcreExec(&parse_regex, str, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, str, 0, 0);
 
     if (ret < 1 || ret > 3) {
         SCLogError("invalid ssh.softwareversion option");
@@ -168,8 +169,7 @@ static DetectSshSoftwareVersionData *DetectSshSoftwareVersionParse (DetectEngine
 
     if (ret > 1) {
         const char *str_ptr = NULL;
-        res = pcre2_substring_get_bynumber(
-                parse_regex.match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+        res = pcre2_substring_get_bynumber(match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
         if (res < 0) {
             SCLogError("pcre2_substring_get_bynumber failed");
             goto error;
@@ -177,11 +177,13 @@ static DetectSshSoftwareVersionData *DetectSshSoftwareVersionParse (DetectEngine
 
         /* We have a correct id option */
         ssh = SCMalloc(sizeof(DetectSshSoftwareVersionData));
-        if (unlikely(ssh == NULL))
+        if (unlikely(ssh == NULL)) {
+            pcre2_substring_free((PCRE2_UCHAR *)str_ptr);
             goto error;
-
+        }
         ssh->software_ver = (uint8_t *)SCStrdup((char *)str_ptr);
         if (ssh->software_ver == NULL) {
+            pcre2_substring_free((PCRE2_UCHAR *)str_ptr);
             goto error;
         }
         pcre2_substring_free((PCRE2_UCHAR *)str_ptr);
@@ -191,9 +193,13 @@ static DetectSshSoftwareVersionData *DetectSshSoftwareVersionParse (DetectEngine
         SCLogDebug("will look for ssh %s", ssh->software_ver);
     }
 
+    pcre2_match_data_free(match);
     return ssh;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     if (ssh != NULL)
         DetectSshSoftwareVersionFree(de_ctx, ssh);
     return NULL;
index b9b6d521567589a452c9ce55f66ec986a27a5cea..3f2df48db7aa441a1abd2efc12ad17d693b4da90 100644 (file)
@@ -140,7 +140,6 @@ static int DetectSslStateMatch(DetectEngineThreadCtx *det_ctx,
  */
 static DetectSslStateData *DetectSslStateParse(const char *arg)
 {
-    int ret = 0, res = 0;
     size_t pcre2len;
     char str1[64];
     char str2[64];
@@ -148,7 +147,8 @@ static DetectSslStateData *DetectSslStateParse(const char *arg)
     uint32_t flags = 0, mask = 0;
     DetectSslStateData *ssd = NULL;
 
-    ret = DetectParsePcreExec(&parse_regex1, arg, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex1, &match, arg, 0, 0);
     if (ret < 1) {
         SCLogError("Invalid arg \"%s\" supplied to "
                    "ssl_state keyword.",
@@ -157,7 +157,7 @@ static DetectSslStateData *DetectSslStateParse(const char *arg)
     }
 
     pcre2len = sizeof(str1);
-    res = pcre2_substring_copy_bynumber(parse_regex1.match, 1, (PCRE2_UCHAR8 *)str1, &pcre2len);
+    int res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)str1, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
@@ -165,7 +165,7 @@ static DetectSslStateData *DetectSslStateParse(const char *arg)
     negate = !strcmp("!", str1);
 
     pcre2len = sizeof(str1);
-    res = pcre2_substring_copy_bynumber(parse_regex1.match, 2, (PCRE2_UCHAR8 *)str1, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)str1, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
@@ -199,32 +199,38 @@ static DetectSslStateData *DetectSslStateParse(const char *arg)
     }
 
     pcre2len = sizeof(str1);
-    res = pcre2_substring_copy_bynumber(parse_regex1.match, 3, (PCRE2_UCHAR8 *)str1, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 3, (PCRE2_UCHAR8 *)str1, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
     }
     while (res >= 0 && strlen(str1) > 0) {
-        ret = DetectParsePcreExec(&parse_regex2, str1, 0, 0);
+        pcre2_match_data *match2 = NULL;
+        ret = DetectParsePcreExec(&parse_regex2, &match2, str1, 0, 0);
         if (ret < 1) {
             SCLogError("Invalid arg \"%s\" supplied to "
                        "ssl_state keyword.",
                     arg);
+            if (match2) {
+                pcre2_match_data_free(match2);
+            }
             goto error;
         }
 
         pcre2len = sizeof(str2);
-        res = pcre2_substring_copy_bynumber(parse_regex2.match, 1, (PCRE2_UCHAR8 *)str2, &pcre2len);
+        res = pcre2_substring_copy_bynumber(match2, 1, (PCRE2_UCHAR8 *)str2, &pcre2len);
         if (res < 0) {
             SCLogError("pcre2_substring_copy_bynumber failed");
+            pcre2_match_data_free(match2);
             goto error;
         }
         negate = !strcmp("!", str2);
 
         pcre2len = sizeof(str2);
-        res = pcre2_substring_copy_bynumber(parse_regex2.match, 2, (PCRE2_UCHAR8 *)str2, &pcre2len);
+        res = pcre2_substring_copy_bynumber(match2, 2, (PCRE2_UCHAR8 *)str2, &pcre2len);
         if (res < 0) {
             SCLogError("pcre2_substring_copy_bynumber failed");
+            pcre2_match_data_free(match2);
             goto error;
         }
         if (strcmp("client_hello", str2) == 0) {
@@ -251,17 +257,20 @@ static DetectSslStateData *DetectSslStateParse(const char *arg)
             SCLogError("Found invalid option \"%s\" "
                        "in ssl_state keyword.",
                     str2);
+            pcre2_match_data_free(match2);
             goto error;
         }
 
         pcre2len = sizeof(str2);
-        res = pcre2_substring_copy_bynumber(parse_regex2.match, 3, (PCRE2_UCHAR8 *)str2, &pcre2len);
+        res = pcre2_substring_copy_bynumber(match2, 3, (PCRE2_UCHAR8 *)str2, &pcre2len);
         if (res < 0) {
             SCLogError("pcre2_substring_copy_bynumber failed");
+            pcre2_match_data_free(match2);
             goto error;
         }
 
         memcpy(str1, str2, sizeof(str1));
+        pcre2_match_data_free(match2);
     }
 
     if ( (ssd = SCMalloc(sizeof(DetectSslStateData))) == NULL) {
@@ -270,9 +279,13 @@ static DetectSslStateData *DetectSslStateParse(const char *arg)
     ssd->flags = flags;
     ssd->mask = mask;
 
+    pcre2_match_data_free(match);
     return ssd;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     return NULL;
 }
 
index ec71acb09f5b90d0e1641a02b7c2a2a2aa96681c..c31b44088d7d57bb4f1f25df702ec893ce972990 100644 (file)
@@ -156,17 +156,17 @@ static int DetectTagMatch(DetectEngineThreadCtx *det_ctx, Packet *p,
 static DetectTagData *DetectTagParse(const char *tagstr)
 {
     DetectTagData td;
-    int ret = 0, res = 0;
     size_t pcre2_len;
     const char *str_ptr = NULL;
 
-    ret = DetectParsePcreExec(&parse_regex, tagstr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, tagstr, 0, 0);
     if (ret < 1) {
         SCLogError("parse error, ret %" PRId32 ", string %s", ret, tagstr);
         goto error;
     }
 
-    res = pcre2_substring_get_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+    int res = pcre2_substring_get_bynumber(match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
     if (res < 0 || str_ptr == NULL) {
         SCLogError("pcre2_substring_get_bynumber failed");
         goto error;
@@ -190,8 +190,7 @@ static DetectTagData *DetectTagParse(const char *tagstr)
     td.direction = DETECT_TAG_DIR_DST;
 
     if (ret > 4) {
-        res = pcre2_substring_get_bynumber(
-                parse_regex.match, 3, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+        res = pcre2_substring_get_bynumber(match, 3, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
         if (res < 0 || str_ptr == NULL) {
             SCLogError("pcre2_substring_get_bynumber failed");
             goto error;
@@ -209,8 +208,7 @@ static DetectTagData *DetectTagParse(const char *tagstr)
         pcre2_substring_free((PCRE2_UCHAR *)str_ptr);
         str_ptr = NULL;
 
-        res = pcre2_substring_get_bynumber(
-                parse_regex.match, 4, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+        res = pcre2_substring_get_bynumber(match, 4, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
         if (res < 0 || str_ptr == NULL) {
             SCLogError("pcre2_substring_get_bynumber failed");
             goto error;
@@ -239,8 +237,7 @@ static DetectTagData *DetectTagParse(const char *tagstr)
 
         /* if specified, overwrite it */
         if (ret == 7) {
-            res = pcre2_substring_get_bynumber(
-                    parse_regex.match, 6, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+            res = pcre2_substring_get_bynumber(match, 6, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
             if (res < 0 || str_ptr == NULL) {
                 SCLogError("pcre2_substring_get_bynumber failed");
                 goto error;
@@ -278,9 +275,13 @@ static DetectTagData *DetectTagParse(const char *tagstr)
     }
 
     memcpy(real_td, &td, sizeof(DetectTagData));
+    pcre2_match_data_free(match);
     return real_td;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     if (str_ptr != NULL)
         pcre2_substring_free((PCRE2_UCHAR *)str_ptr);
     return NULL;
index f7ce19794e2a1959a6f04ba2ed5f56985a7864aa..b34b6e0d70667d9671c3318b95ef69293b4e062f 100644 (file)
@@ -81,41 +81,48 @@ void DetectTargetRegister(void) {
  */
 static int DetectTargetParse(Signature *s, const char *targetstr)
 {
-    int ret = 0, res = 0;
     size_t pcre2len;
     char value[10];
 
-    ret = DetectParsePcreExec(&parse_regex, targetstr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, targetstr, 0, 0);
     if (ret < 1) {
         SCLogError("pcre_exec parse error, ret %" PRId32 ", string %s", ret, targetstr);
-        return -1;
+        goto error;
     }
 
     pcre2len = sizeof(value);
-    res = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)value, &pcre2len);
+    int res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)value, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
-        return -1;
+        goto error;
     }
 
     /* now check key value */
     if (!strcmp(value, "src_ip")) {
         if (s->flags & SIG_FLAG_DEST_IS_TARGET) {
             SCLogError("Conflicting values of target keyword");
-            return -1;
+            goto error;
         }
         s->flags |= SIG_FLAG_SRC_IS_TARGET;
     } else if (!strcmp(value, "dest_ip")) {
         if (s->flags & SIG_FLAG_SRC_IS_TARGET) {
             SCLogError("Conflicting values of target keyword");
-            return -1;
+            goto error;
         }
         s->flags |= SIG_FLAG_DEST_IS_TARGET;
     } else {
         SCLogError("only 'src_ip' and 'dest_ip' are supported as target value");
-        return -1;
+        goto error;
     }
+    pcre2_match_data_free(match);
     return 0;
+
+error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
+    return -1;
 }
 
 /**
index 23a3bd23b0d7c46e226f203b4bc4b3b9ed19191b..183ae96f679293a3360331fbef7b28e5f5c08ae6 100644 (file)
@@ -173,51 +173,52 @@ static DetectFlagsData *DetectFlagsParse (const char *rawstr)
 {
     SCEnter();
 
-    int ret = 0, found = 0, ignore = 0, res = 0;
-    size_t pcre2len;
+    int found = 0, ignore = 0;
     char *ptr;
+    DetectFlagsData *de = NULL;
 
     char arg1[16] = "";
     char arg2[16] = "";
     char arg3[16] = "";
 
-    ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     SCLogDebug("input '%s', pcre said %d", rawstr, ret);
     if (ret < 3) {
         SCLogError("pcre match failed");
-        SCReturnPtr(NULL, "DetectFlagsData");
+        goto error;
     }
 
-    pcre2len = sizeof(arg1);
-    res = SC_Pcre2SubstringCopy(parse_regex.match, 1, (PCRE2_UCHAR8 *)arg1, &pcre2len);
+    size_t pcre2len = sizeof(arg1);
+    int res = SC_Pcre2SubstringCopy(match, 1, (PCRE2_UCHAR8 *)arg1, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
-        SCReturnPtr(NULL, "DetectFlagsData");
+        goto error;
     }
     if (ret >= 2) {
         pcre2len = sizeof(arg2);
-        res = pcre2_substring_copy_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 *)arg2, &pcre2len);
+        res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)arg2, &pcre2len);
         if (res < 0) {
             SCLogError("pcre2_substring_copy_bynumber failed");
-            SCReturnPtr(NULL, "DetectFlagsData");
+            goto error;
         }
     }
     if (ret >= 3) {
         pcre2len = sizeof(arg3);
-        res = SC_Pcre2SubstringCopy(parse_regex.match, 3, (PCRE2_UCHAR8 *)arg3, &pcre2len);
+        res = SC_Pcre2SubstringCopy(match, 3, (PCRE2_UCHAR8 *)arg3, &pcre2len);
         if (res < 0) {
             SCLogError("pcre2_substring_copy_bynumber failed");
-            SCReturnPtr(NULL, "DetectFlagsData");
+            goto error;
         }
     }
     SCLogDebug("args '%s', '%s', '%s'", arg1, arg2, arg3);
 
     if (strlen(arg2) == 0) {
         SCLogDebug("empty argument");
-        SCReturnPtr(NULL, "DetectFlagsData");
+        goto error;
     }
 
-    DetectFlagsData *de = SCMalloc(sizeof(DetectFlagsData));
+    de = SCMalloc(sizeof(DetectFlagsData));
     if (unlikely(de == NULL))
         goto error;
     memset(de, 0, sizeof(DetectFlagsData));
@@ -450,6 +451,7 @@ static DetectFlagsData *DetectFlagsParse (const char *rawstr)
         }
     }
 
+    pcre2_match_data_free(match);
     SCLogDebug("found %"PRId32" ignore %"PRId32"", found, ignore);
     SCReturnPtr(de, "DetectFlagsData");
 
@@ -457,6 +459,9 @@ error:
     if (de) {
         SCFree(de);
     }
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     SCReturnPtr(NULL, "DetectFlagsData");
 }
 
index c0817c93bff557280d9b017f019fa12a3d7dace4..9f5c56270bb506a5f4f1d7661ea99a08b4090352 100644 (file)
@@ -110,10 +110,11 @@ static int DetectWindowMatch(DetectEngineThreadCtx *det_ctx, Packet *p,
 static DetectWindowData *DetectWindowParse(DetectEngineCtx *de_ctx, const char *windowstr)
 {
     DetectWindowData *wd = NULL;
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2len;
 
-    ret = DetectParsePcreExec(&parse_regex, windowstr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, windowstr, 0, 0);
     if (ret < 1 || ret > 3) {
         SCLogError("pcre_exec parse error, ret %" PRId32 ", string %s", ret, windowstr);
         goto error;
@@ -126,7 +127,7 @@ static DetectWindowData *DetectWindowParse(DetectEngineCtx *de_ctx, const char *
     if (ret > 1) {
         char copy_str[128] = "";
         pcre2len = sizeof(copy_str);
-        res = SC_Pcre2SubstringCopy(parse_regex.match, 1, (PCRE2_UCHAR8 *)copy_str, &pcre2len);
+        res = SC_Pcre2SubstringCopy(match, 1, (PCRE2_UCHAR8 *)copy_str, &pcre2len);
         if (res < 0) {
             SCLogError("pcre2_substring_copy_bynumber failed");
             goto error;
@@ -140,8 +141,7 @@ static DetectWindowData *DetectWindowParse(DetectEngineCtx *de_ctx, const char *
 
         if (ret > 2) {
             pcre2len = sizeof(copy_str);
-            res = pcre2_substring_copy_bynumber(
-                    parse_regex.match, 2, (PCRE2_UCHAR8 *)copy_str, &pcre2len);
+            res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)copy_str, &pcre2len);
             if (res < 0) {
                 SCLogError("pcre2_substring_copy_bynumber failed");
                 goto error;
@@ -155,9 +155,13 @@ static DetectWindowData *DetectWindowParse(DetectEngineCtx *de_ctx, const char *
         }
     }
 
+    pcre2_match_data_free(match);
     return wd;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     if (wd != NULL)
         DetectWindowFree(de_ctx, wd);
     return NULL;
index e43b045f91e6f0666a52fdd40ccf101de629a903..693e4bde821b8e28c3a51ec2b27442349f870826 100644 (file)
@@ -129,43 +129,50 @@ static DetectTemplateData *DetectTemplateParse (const char *templatestr)
 {
     char arg1[4] = "";
     char arg2[4] = "";
-    size_t pcre2len;
 
-    int ret = DetectParsePcreExec(&parse_regex, templatestr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, templatestr, 0, 0);
     if (ret != 3) {
         SCLogError("parse error, ret %" PRId32 "", ret);
-        return NULL;
+        goto error;
     }
 
-    pcre2len = sizeof(arg1);
-    ret = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)arg1, &pcre2len);
+    size_t pcre2len = sizeof(arg1);
+    ret = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)arg1, &pcre2len);
     if (ret < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
-        return NULL;
+        goto error;
     }
     SCLogDebug("Arg1 \"%s\"", arg1);
 
     pcre2len = sizeof(arg2);
-    ret = pcre2_substring_copy_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 *)arg2, &pcre2len);
+    ret = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)arg2, &pcre2len);
     if (ret < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
-        return NULL;
+        goto error;
     }
     SCLogDebug("Arg2 \"%s\"", arg2);
 
     DetectTemplateData *templated = SCMalloc(sizeof (DetectTemplateData));
     if (unlikely(templated == NULL))
-        return NULL;
+        goto error;
 
     if (ByteExtractStringUint8(&templated->arg1, 10, 0, (const char *)arg1) < 0) {
         SCFree(templated);
-        return NULL;
+        goto error;
     }
     if (ByteExtractStringUint8(&templated->arg2, 10, 0, (const char *)arg2) < 0) {
         SCFree(templated);
-        return NULL;
+        goto error;
     }
+    pcre2_match_data_free(match);
     return templated;
+
+error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
+    return NULL;
 }
 
 /**
index 25bda3556bc4510e1a56cc0e1103772ef44d250c..95a09633b2ba94d6fabdcd0777ea062b8250dc6d 100644 (file)
@@ -121,6 +121,7 @@ static DetectThresholdData *DetectThresholdParse(const char *rawstr)
     int second_pos = 0, count_pos = 0;
     size_t pos = 0;
     int i = 0;
+    pcre2_match_data *match = NULL;
 
     copy_str = SCStrdup(rawstr);
     if (unlikely(copy_str == NULL)) {
@@ -147,7 +148,7 @@ static DetectThresholdData *DetectThresholdParse(const char *rawstr)
     if(count_found != 1 || second_found != 1 || type_found != 1 || track_found != 1)
         goto error;
 
-    ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     if (ret < 5) {
         SCLogError("pcre_exec parse error, ret %" PRId32 ", string %s", ret, rawstr);
         goto error;
@@ -161,8 +162,7 @@ static DetectThresholdData *DetectThresholdParse(const char *rawstr)
 
     for (i = 0; i < (ret - 1); i++) {
 
-        res = pcre2_substring_get_bynumber(
-                parse_regex.match, i + 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+        res = pcre2_substring_get_bynumber(match, i + 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
 
         if (res < 0) {
             SCLogError("pcre2_substring_get_bynumber failed");
@@ -209,9 +209,13 @@ static DetectThresholdData *DetectThresholdParse(const char *rawstr)
         if (args[i] != NULL)
             pcre2_substring_free((PCRE2_UCHAR8 *)args[i]);
     }
+    pcre2_match_data_free(match);
     return de;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     for (i = 0; i < (ret - 1); i++){
         if (args[i] != NULL)
             pcre2_substring_free((PCRE2_UCHAR8 *)args[i]);
index 4135e328bac6cc7d7ab7f31493c3415eb4457c9f..63939b84928679c5ff38cab95398ad9b96eb199d 100644 (file)
@@ -290,21 +290,20 @@ static time_t DateStringToEpoch (char *string)
 static DetectTlsValidityData *DetectTlsValidityParse (const char *rawstr)
 {
     DetectTlsValidityData *dd = NULL;
-    int ret = 0, res = 0;
-    size_t pcre2len;
     char mode[2] = "";
     char value1[20] = "";
     char value2[20] = "";
     char range[3] = "";
 
-    ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     if (ret < 3 || ret > 5) {
         SCLogError("Parse error %s", rawstr);
         goto error;
     }
 
-    pcre2len = sizeof(mode);
-    res = SC_Pcre2SubstringCopy(parse_regex.match, 1, (PCRE2_UCHAR8 *)mode, &pcre2len);
+    size_t pcre2len = sizeof(mode);
+    int res = SC_Pcre2SubstringCopy(match, 1, (PCRE2_UCHAR8 *)mode, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
@@ -312,7 +311,7 @@ static DetectTlsValidityData *DetectTlsValidityParse (const char *rawstr)
     SCLogDebug("mode \"%s\"", mode);
 
     pcre2len = sizeof(value1);
-    res = pcre2_substring_copy_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 *)value1, &pcre2len);
+    res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)value1, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
@@ -321,7 +320,7 @@ static DetectTlsValidityData *DetectTlsValidityParse (const char *rawstr)
 
     if (ret > 3) {
         pcre2len = sizeof(range);
-        res = pcre2_substring_copy_bynumber(parse_regex.match, 3, (PCRE2_UCHAR8 *)range, &pcre2len);
+        res = pcre2_substring_copy_bynumber(match, 3, (PCRE2_UCHAR8 *)range, &pcre2len);
         if (res < 0) {
             SCLogError("pcre2_substring_copy_bynumber failed");
             goto error;
@@ -330,8 +329,7 @@ static DetectTlsValidityData *DetectTlsValidityParse (const char *rawstr)
 
         if (ret > 4) {
             pcre2len = sizeof(value2);
-            res = pcre2_substring_copy_bynumber(
-                    parse_regex.match, 4, (PCRE2_UCHAR8 *)value2, &pcre2len);
+            res = pcre2_substring_copy_bynumber(match, 4, (PCRE2_UCHAR8 *)value2, &pcre2len);
             if (res < 0) {
                 SCLogError("pcre2_substring_copy_bynumber failed");
                 goto error;
@@ -390,9 +388,13 @@ static DetectTlsValidityData *DetectTlsValidityParse (const char *rawstr)
             goto error;
         }
     }
+    pcre2_match_data_free(match);
     return dd;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     if (dd)
         SCFree(dd);
     return NULL;
index ac663c1fea9591aaf251dd270d774756f4154046..cba1f55e95d182419e4faaa59a1e5181769584cb 100644 (file)
@@ -150,10 +150,11 @@ static DetectTlsVersionData *DetectTlsVersionParse (DetectEngineCtx *de_ctx, con
 {
     uint16_t temp;
     DetectTlsVersionData *tls = NULL;
-    int ret = 0, res = 0;
+    int res = 0;
     size_t pcre2len;
 
-    ret = DetectParsePcreExec(&parse_regex, str, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, str, 0, 0);
     if (ret < 1 || ret > 3) {
         SCLogError("invalid tls.version option");
         goto error;
@@ -163,8 +164,7 @@ static DetectTlsVersionData *DetectTlsVersionParse (DetectEngineCtx *de_ctx, con
         char ver_ptr[64];
         char *tmp_str;
         pcre2len = sizeof(ver_ptr);
-        res = pcre2_substring_copy_bynumber(
-                parse_regex.match, 1, (PCRE2_UCHAR8 *)ver_ptr, &pcre2len);
+        res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)ver_ptr, &pcre2len);
         if (res < 0) {
             SCLogError("pcre2_substring_copy_bynumber failed");
             goto error;
@@ -205,9 +205,13 @@ static DetectTlsVersionData *DetectTlsVersionParse (DetectEngineCtx *de_ctx, con
         SCLogDebug("will look for tls %"PRIu16"", tls->ver);
     }
 
+    pcre2_match_data_free(match);
     return tls;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     if (tls != NULL)
         DetectTlsVersionFree(de_ctx, tls);
     return NULL;
index b04f4398bfa77a08095bd18d71f427afada16935..71e45696cd9c10dc4bb4514a124e005a4a962bc0 100644 (file)
@@ -217,14 +217,14 @@ static int DetectTlsSubjectMatch (DetectEngineThreadCtx *det_ctx,
 static DetectTlsData *DetectTlsSubjectParse (DetectEngineCtx *de_ctx, const char *str, bool negate)
 {
     DetectTlsData *tls = NULL;
-    int ret = 0, res = 0;
     size_t pcre2_len;
     const char *str_ptr;
     char *orig = NULL;
     char *tmp_str;
     uint32_t flag = 0;
 
-    ret = DetectParsePcreExec(&subject_parse_regex, str, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&subject_parse_regex, &match, str, 0, 0);
     if (ret != 2) {
         SCLogError("invalid tls.subject option");
         goto error;
@@ -233,8 +233,7 @@ static DetectTlsData *DetectTlsSubjectParse (DetectEngineCtx *de_ctx, const char
     if (negate)
         flag = DETECT_CONTENT_NEGATED;
 
-    res = pcre2_substring_get_bynumber(
-            subject_parse_regex.match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+    int res = pcre2_substring_get_bynumber(match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
     if (res < 0) {
         SCLogError("pcre2_substring_get_bynumber failed");
         goto error;
@@ -266,6 +265,7 @@ static DetectTlsData *DetectTlsSubjectParse (DetectEngineCtx *de_ctx, const char
         goto error;
     }
 
+    pcre2_match_data_free(match);
     SCFree(orig);
 
     SCLogDebug("will look for TLS subject %s", tls->subject);
@@ -273,6 +273,9 @@ static DetectTlsData *DetectTlsSubjectParse (DetectEngineCtx *de_ctx, const char
     return tls;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     if (orig != NULL)
         SCFree(orig);
     if (tls != NULL)
@@ -409,14 +412,14 @@ static int DetectTlsIssuerDNMatch (DetectEngineThreadCtx *det_ctx,
 static DetectTlsData *DetectTlsIssuerDNParse(DetectEngineCtx *de_ctx, const char *str, bool negate)
 {
     DetectTlsData *tls = NULL;
-    int ret = 0, res = 0;
     size_t pcre2_len;
     const char *str_ptr;
     char *orig = NULL;
     char *tmp_str;
     uint32_t flag = 0;
 
-    ret = DetectParsePcreExec(&issuerdn_parse_regex, str, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&issuerdn_parse_regex, &match, str, 0, 0);
     if (ret != 2) {
         SCLogError("invalid tls.issuerdn option");
         goto error;
@@ -425,8 +428,7 @@ static DetectTlsData *DetectTlsIssuerDNParse(DetectEngineCtx *de_ctx, const char
     if (negate)
         flag = DETECT_CONTENT_NEGATED;
 
-    res = pcre2_substring_get_bynumber(
-            issuerdn_parse_regex.match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
+    int res = pcre2_substring_get_bynumber(match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
     if (res < 0) {
         SCLogError("pcre2_substring_get_bynumber failed");
         goto error;
@@ -461,11 +463,15 @@ static DetectTlsData *DetectTlsIssuerDNParse(DetectEngineCtx *de_ctx, const char
 
     SCFree(orig);
 
+    pcre2_match_data_free(match);
     SCLogDebug("Will look for TLS issuerdn %s", tls->issuerdn);
 
     return tls;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     if (orig != NULL)
         SCFree(orig);
     if (tls != NULL)
index 422882f1f9a8832e9e1495a2a6e81fed4f4ce4de..002ff9c927c97976a60f0d1a05e6d33ff87ed9ef 100644 (file)
@@ -111,10 +111,10 @@ static int DetectTosMatch(DetectEngineThreadCtx *det_ctx, Packet *p,
 static DetectTosData *DetectTosParse(const char *arg, bool negate)
 {
     DetectTosData *tosd = NULL;
-    int ret = 0, res = 0;
     size_t pcre2len;
 
-    ret = DetectParsePcreExec(&parse_regex, arg, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, arg, 0, 0);
     if (ret != 2) {
         SCLogError("invalid tos option - %s. "
                    "The tos option value must be in the range "
@@ -126,8 +126,7 @@ static DetectTosData *DetectTosParse(const char *arg, bool negate)
     /* For TOS value */
     char tosbytes_str[64] = "";
     pcre2len = sizeof(tosbytes_str);
-    res = pcre2_substring_copy_bynumber(
-            parse_regex.match, 1, (PCRE2_UCHAR8 *)tosbytes_str, &pcre2len);
+    int res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)tosbytes_str, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
         goto error;
@@ -159,9 +158,13 @@ static DetectTosData *DetectTosParse(const char *arg, bool negate)
     tosd->tos = (uint8_t)tos;
     tosd->negated = negate;
 
+    pcre2_match_data_free(match);
     return tosd;
 
 error:
+    if (match) {
+        pcre2_match_data_free(match);
+    }
     return NULL;
 }
 
index 787b2dd50bfbba736c9f86db0fcbe1f44d874019..9c5871f7af8fd643472d27b6e750c857cee3ced6 100644 (file)
@@ -194,41 +194,44 @@ static int DetectXbitParse(DetectEngineCtx *de_ctx,
     DetectXbitsData *cd = NULL;
     uint8_t fb_cmd = 0;
     uint8_t hb_dir = 0;
-    int ret = 0, res = 0;
     size_t pcre2len;
     char fb_cmd_str[16] = "", fb_name[256] = "";
     char hb_dir_str[16] = "";
     enum VarTypes var_type = VAR_TYPE_NOT_SET;
     uint32_t expire = DETECT_XBITS_EXPIRE_DEFAULT;
 
-    ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
+    pcre2_match_data *match = NULL;
+    int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
     if (ret != 2 && ret != 3 && ret != 4 && ret != 5) {
         SCLogError("\"%s\" is not a valid setting for xbits.", rawstr);
+        if (match) {
+            pcre2_match_data_free(match);
+        }
         return -1;
     }
     SCLogDebug("ret %d, %s", ret, rawstr);
     pcre2len = sizeof(fb_cmd_str);
-    res = pcre2_substring_copy_bynumber(
-            parse_regex.match, 1, (PCRE2_UCHAR8 *)fb_cmd_str, &pcre2len);
+    int res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)fb_cmd_str, &pcre2len);
     if (res < 0) {
         SCLogError("pcre2_substring_copy_bynumber failed");
+        pcre2_match_data_free(match);
         return -1;
     }
 
     if (ret >= 3) {
         pcre2len = sizeof(fb_name);
-        res = pcre2_substring_copy_bynumber(
-                parse_regex.match, 2, (PCRE2_UCHAR8 *)fb_name, &pcre2len);
+        res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)fb_name, &pcre2len);
         if (res < 0) {
             SCLogError("pcre2_substring_copy_bynumber failed");
+            pcre2_match_data_free(match);
             return -1;
         }
         if (ret >= 4) {
             pcre2len = sizeof(hb_dir_str);
-            res = pcre2_substring_copy_bynumber(
-                    parse_regex.match, 3, (PCRE2_UCHAR8 *)hb_dir_str, &pcre2len);
+            res = pcre2_substring_copy_bynumber(match, 3, (PCRE2_UCHAR8 *)hb_dir_str, &pcre2len);
             if (res < 0) {
                 SCLogError("pcre2_substring_copy_bynumber failed");
+                pcre2_match_data_free(match);
                 return -1;
             }
             SCLogDebug("hb_dir_str %s", hb_dir_str);
@@ -244,6 +247,7 @@ static int DetectXbitParse(DetectEngineCtx *de_ctx,
                     var_type = VAR_TYPE_IPPAIR_BIT;
                 } else {
                     // TODO
+                    pcre2_match_data_free(match);
                     return -1;
                 }
             }
@@ -252,9 +256,10 @@ static int DetectXbitParse(DetectEngineCtx *de_ctx,
                 char expire_str[16] = "";
                 pcre2len = sizeof(expire_str);
                 res = pcre2_substring_copy_bynumber(
-                        parse_regex.match, 4, (PCRE2_UCHAR8 *)expire_str, &pcre2len);
+                        match, 4, (PCRE2_UCHAR8 *)expire_str, &pcre2len);
                 if (res < 0) {
                     SCLogError("pcre2_substring_copy_bynumber failed");
+                    pcre2_match_data_free(match);
                     return -1;
                 }
                 SCLogDebug("expire_str %s", expire_str);
@@ -262,10 +267,12 @@ static int DetectXbitParse(DetectEngineCtx *de_ctx,
                     SCLogError("Invalid value for "
                                "expire: \"%s\"",
                             expire_str);
+                    pcre2_match_data_free(match);
                     return -1;
                 }
                 if (expire == 0) {
                     SCLogError("expire must be bigger than 0");
+                    pcre2_match_data_free(match);
                     return -1;
                 }
                 SCLogDebug("expire %d", expire);
@@ -273,6 +280,7 @@ static int DetectXbitParse(DetectEngineCtx *de_ctx,
         }
     }
 
+    pcre2_match_data_free(match);
     if (strcmp(fb_cmd_str,"noalert") == 0) {
         fb_cmd = DETECT_XBITS_CMD_NOALERT;
     } else if (strcmp(fb_cmd_str,"isset") == 0) {