Skip to content

Commit

Permalink
pcre: local match data for pcrexform
Browse files Browse the repository at this point in the history
  • Loading branch information
catenacyber authored and victorjulien committed Oct 1, 2021
1 parent c64a1f6 commit 586522e
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/detect-transform-pcrexform.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

typedef struct DetectTransformPcrexformData {
pcre2_code *regex;
pcre2_match_data *match_data;
} DetectTransformPcrexformData;

static int DetectTransformPcrexformSetup (DetectEngineCtx *, Signature *, const char *);
Expand Down Expand Up @@ -65,7 +64,6 @@ static void DetectTransformPcrexformFree(DetectEngineCtx *de_ctx, void *ptr)
if (ptr != NULL) {
DetectTransformPcrexformData *pxd = (DetectTransformPcrexformData *) ptr;
pcre2_code_free(pxd->regex);
pcre2_match_data_free(pxd->match_data);
SCFree(pxd);
}
}
Expand Down Expand Up @@ -116,7 +114,6 @@ static int DetectTransformPcrexformSetup (DetectEngineCtx *de_ctx, Signature *s,
DetectTransformPcrexformFree(de_ctx, pxd);
SCReturnInt(-1);
}
pxd->match_data = pcre2_match_data_create_from_pattern(pxd->regex, NULL);

int r = DetectSignatureAddTransform(s, DETECT_TRANSFORM_PCREXFORM, pxd);
if (r != 0) {
Expand All @@ -132,18 +129,20 @@ static void DetectTransformPcrexform(InspectionBuffer *buffer, void *options)
const uint32_t input_len = buffer->inspect_len;
DetectTransformPcrexformData *pxd = options;

int ret = pcre2_match(pxd->regex, (PCRE2_SPTR8)input, input_len, 0, 0, pxd->match_data, NULL);
pcre2_match_data *match = pcre2_match_data_create_from_pattern(pxd->regex, NULL);
int ret = pcre2_match(pxd->regex, (PCRE2_SPTR8)input, input_len, 0, 0, match, NULL);

if (ret > 0) {
const char *str;
PCRE2_SIZE caplen;
ret = pcre2_substring_get_bynumber(pxd->match_data, 0, (PCRE2_UCHAR8 **)&str, &caplen);
ret = pcre2_substring_get_bynumber(match, 0, (PCRE2_UCHAR8 **)&str, &caplen);

if (ret >= 0) {
InspectionBufferCopy(buffer, (uint8_t *)str, (uint32_t)caplen);
pcre2_substring_free((PCRE2_UCHAR8 *)str);
}
}
pcre2_match_data_free(match);
}

#ifdef UNITTESTS
Expand Down

0 comments on commit 586522e

Please sign in to comment.