Skip to content

Commit 0362af4

Browse files
committed
Move PCRE2 match block from member variable
1 parent 770662c commit 0362af4

File tree

5 files changed

+41
-27
lines changed

5 files changed

+41
-27
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
v3.x.y - YYYY-MMM-DD (to be released)
22
-------------------------------------
33

4+
- Move PCRE2 match block from member variable
5+
[Issue #2745 - @martinhsv]
46
- Add SecArgumentsLimit, 200007 to modsecurity.conf-recommended
57
[Issue #2738 - @jleproust, @martinhsv]
68
- Fix memory leak when concurrent log includes REMOTE_USER

src/operators/verify_cc.cc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ bool VerifyCC::init(const std::string &param2, std::string *error) {
104104
pcre2_options, &errornumber, &erroroffset, NULL);
105105
if (m_pc == NULL) {
106106
return false;
107-
} else {
108-
m_match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
109-
if (m_match_data == NULL) {
110-
return false;
111-
}
112107
}
113108
#else
114109
const char *errptr = NULL;
@@ -145,15 +140,16 @@ bool VerifyCC::evaluate(Transaction *t, RuleWithActions *rule,
145140
PCRE2_SIZE offset = 0;
146141
size_t target_length = i.length();
147142
PCRE2_SPTR pcre2_i = reinterpret_cast<PCRE2_SPTR>(i.c_str());
143+
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
148144

149145
for (offset = 0; offset < target_length; offset++) {
150-
int ret = pcre2_match(m_pc, pcre2_i, target_length, offset, 0, m_match_data, NULL);
146+
int ret = pcre2_match(m_pc, pcre2_i, target_length, offset, 0, match_data, NULL);
151147

152148
/* If there was no match, then we are done. */
153149
if (ret < 0) {
154150
break;
155151
}
156-
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(m_match_data);
152+
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
157153

158154
#else
159155
int offset = 0;
@@ -188,11 +184,18 @@ bool VerifyCC::evaluate(Transaction *t, RuleWithActions *rule,
188184
"\" at " + i + ". [offset " +
189185
std::to_string(offset) + "]");
190186
}
187+
#ifdef WITH_PCRE2
188+
pcre2_match_data_free(match_data);
189+
#endif
191190
return true;
192191
}
193192
}
194193
}
195194

195+
#ifdef WITH_PCRE2
196+
pcre2_match_data_free(match_data);
197+
#endif
198+
196199
return false;
197200
}
198201

src/operators/verify_cc.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ class VerifyCC : public Operator {
3838
/** @ingroup ModSecurity_Operator */
3939
explicit VerifyCC(std::unique_ptr<RunTimeString> param)
4040
: Operator("VerifyCC", std::move(param)),
41-
m_pc(NULL),
4241
#if WITH_PCRE2
43-
m_match_data(NULL) { }
42+
m_pc(NULL) { }
4443
#else
44+
m_pc(NULL),
4545
m_pce(NULL) { }
4646
#endif
4747
~VerifyCC();
@@ -53,7 +53,6 @@ class VerifyCC : public Operator {
5353
private:
5454
#if WITH_PCRE2
5555
pcre2_code *m_pc;
56-
pcre2_match_data *m_match_data;
5756
#else
5857
pcre *m_pc;
5958
pcre_extra *m_pce;

src/utils/regex.cc

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,6 @@ Regex::Regex(const std::string& pattern_, bool ignoreCase)
7373
PCRE2_SIZE erroroffset = 0;
7474
m_pc = pcre2_compile(pcre2_pattern, PCRE2_ZERO_TERMINATED,
7575
pcre2_options, &errornumber, &erroroffset, NULL);
76-
if (m_pc != NULL) {
77-
m_match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
78-
if (m_match_data == NULL) {
79-
m_pc = NULL;
80-
}
81-
}
8276
#else
8377
const char *errptr = NULL;
8478
int erroffset;
@@ -97,7 +91,6 @@ Regex::Regex(const std::string& pattern_, bool ignoreCase)
9791

9892
Regex::~Regex() {
9993
#if WITH_PCRE2
100-
pcre2_match_data_free(m_match_data);
10194
pcre2_code_free(m_pc);
10295
#else
10396
if (m_pc != NULL) {
@@ -123,10 +116,11 @@ std::list<SMatch> Regex::searchAll(const std::string& s) const {
123116
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
124117
PCRE2_SIZE offset = 0;
125118

119+
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
126120
do {
127121
rc = pcre2_match(m_pc, pcre2_s, s.length(),
128-
offset, 0, m_match_data, NULL);
129-
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(m_match_data);
122+
offset, 0, match_data, NULL);
123+
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
130124
#else
131125
const char *subject = s.c_str();
132126
int ovector[OVECCOUNT];
@@ -155,14 +149,18 @@ std::list<SMatch> Regex::searchAll(const std::string& s) const {
155149
}
156150
} while (rc > 0);
157151

152+
#ifdef WITH_PCRE2
153+
pcre2_match_data_free(match_data);
154+
#endif
158155
return retList;
159156
}
160157

161158
bool Regex::searchOneMatch(const std::string& s, std::vector<SMatchCapture>& captures) const {
162159
#ifdef WITH_PCRE2
163160
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
164-
int rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, 0, m_match_data, NULL);
165-
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(m_match_data);
161+
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
162+
int rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, NULL);
163+
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
166164
#else
167165
const char *subject = s.c_str();
168166
int ovector[OVECCOUNT];
@@ -181,6 +179,9 @@ bool Regex::searchOneMatch(const std::string& s, std::vector<SMatchCapture>& cap
181179
captures.push_back(capture);
182180
}
183181

182+
#ifdef WITH_PCRE2
183+
pcre2_match_data_free(match_data);
184+
#endif
184185
return (rc > 0);
185186
}
186187

@@ -190,14 +191,15 @@ bool Regex::searchGlobal(const std::string& s, std::vector<SMatchCapture>& captu
190191
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
191192
PCRE2_SIZE startOffset = 0;
192193

194+
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
193195
while (startOffset <= s.length()) {
194196
uint32_t pcre2_options = 0;
195197
if (prev_match_zero_length) {
196198
pcre2_options = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED;
197199
}
198200
int rc = pcre2_match(m_pc, pcre2_s, s.length(),
199-
startOffset, pcre2_options, m_match_data, NULL);
200-
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(m_match_data);
201+
startOffset, pcre2_options, match_data, NULL);
202+
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
201203

202204
#else
203205
const char *subject = s.c_str();
@@ -258,17 +260,21 @@ bool Regex::searchGlobal(const std::string& s, std::vector<SMatchCapture>& captu
258260
}
259261
}
260262

263+
#ifdef WITH_PCRE2
264+
pcre2_match_data_free(match_data);
265+
#endif
261266
return (captures.size() > 0);
262267
}
263268

264269
int Regex::search(const std::string& s, SMatch *match) const {
265270
#ifdef WITH_PCRE2
266271
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
272+
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
267273
int ret = pcre2_match(m_pc, pcre2_s, s.length(),
268-
0, 0, m_match_data, NULL) > 0;
274+
0, 0, match_data, NULL) > 0;
269275

270276
if (ret > 0) { // match
271-
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(m_match_data);
277+
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
272278
#else
273279
int ovector[OVECCOUNT];
274280
int ret = pcre_exec(m_pc, m_pce, s.c_str(),
@@ -281,13 +287,18 @@ int Regex::search(const std::string& s, SMatch *match) const {
281287
0);
282288
}
283289

290+
#ifdef WITH_PCRE2
291+
pcre2_match_data_free(match_data);
292+
#endif
284293
return ret;
285294
}
286295

287296
int Regex::search(const std::string& s) const {
288297
#ifdef WITH_PCRE2
289298
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
290-
int rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, 0, m_match_data, NULL);
299+
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
300+
int rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, NULL);
301+
pcre2_match_data_free(match_data);
291302
if (rc > 0) {
292303
return 1; // match
293304
} else {

src/utils/regex.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ class Regex {
8585
private:
8686
#if WITH_PCRE2
8787
pcre2_code *m_pc;
88-
pcre2_match_data *m_match_data;
8988
#else
9089
pcre *m_pc = NULL;
9190
pcre_extra *m_pce = NULL;

0 commit comments

Comments
 (0)