Skip to content

Commit c9fb43e

Browse files
authored
ControlMatcher: error versus NOMATCH checking (#12551)
This updates the ControlMatcher logic to distinguish between error conditions versus NOMATCH conditions. Without this patch, ControlMatcher was emitting incorrect regex matching warnings for NOMATCH conditions.
1 parent 9c2c816 commit c9fb43e

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

src/proxy/ControlMatcher.cc

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -476,13 +476,15 @@ RegexMatcher<Data, MatchResult>::Match(RequestData *rdata, MatchResult *result)
476476
// The function unescapifyStr() is already called in
477477
// HttpRequestData::get_string(); therefore, no need to call again here.
478478
for (int i = 0; i < num_el; i++) {
479-
if (regex_array[i].exec(url_str) == true) {
479+
RegexMatches matches;
480+
int r = regex_array[i].exec(url_str, matches);
481+
if (r >= 0) {
480482
Dbg(dbg_ctl_matcher, "%s Matched %s with regex at line %d", matcher_name, url_str, data_array[i].line_num);
481483
data_array[i].UpdateMatch(result, rdata);
482-
} else {
484+
} else if (r != PCRE2_ERROR_NOMATCH) {
483485
// An error has occurred
484-
Warning("Error matching regex at line %d.", data_array[i].line_num);
485-
} // else it's -1 which means no match was found.
486+
Warning("Error matching regex for url: %s:%d (%d)", file_name ? file_name : "unknown", data_array[i].line_num, r);
487+
} // else: PCRE2_ERROR_NOMATCH
486488
}
487489
ats_free(url_str);
488490
}
@@ -521,14 +523,17 @@ HostRegexMatcher<Data, MatchResult>::Match(RequestData *rdata, MatchResult *resu
521523
url_str = "";
522524
}
523525
for (int i = 0; i < num_el; i++) {
524-
if (this->regex_array[i].exec(url_str) == true) {
526+
RegexMatches matches;
527+
int r = this->regex_array[i].exec(url_str, matches);
528+
if (r >= 0) {
525529
Dbg(dbg_ctl_matcher, "%s Matched %s with regex at line %d", const_cast<char *>(this->matcher_name), url_str,
526530
this->data_array[i].line_num);
527531
this->data_array[i].UpdateMatch(result, rdata);
528-
} else {
532+
} else if (r != PCRE2_ERROR_NOMATCH) {
529533
// An error has occurred
530-
Warning("error matching regex at line %d", this->data_array[i].line_num);
531-
}
534+
Warning("Error matching regex for host: %s:%d (%d)", this->file_name ? this->file_name : "unknown",
535+
this->data_array[i].line_num, r);
536+
} // else: PCRE2_ERROR_NOMATCH
532537
}
533538
}
534539

src/tsutil/Regex.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ Regex::exec(std::string_view subject, uint32_t flags) const
312312
RegexMatches matches;
313313

314314
int count = this->exec(subject, matches, flags);
315-
return count > 0;
315+
return count >= 0;
316316
}
317317

318318
//----------------------------------------------------------------------------
@@ -330,7 +330,7 @@ Regex::exec(std::string_view subject, RegexMatches &matches, uint32_t flags) con
330330

331331
// check if there is a compiled regex
332332
if (code == nullptr) {
333-
return 0;
333+
return PCRE2_ERROR_NULL;
334334
}
335335
int count = pcre2_match(code, reinterpret_cast<PCRE2_SPTR>(subject.data()), subject.size(), 0, flags,
336336
RegexMatches::_MatchData::get(matches._match_data), RegexContext::get_instance()->get_match_context());

src/tsutil/unit_tests/test_Regex.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#include <string_view>
2424
#include <vector>
2525

26+
#define PCRE2_CODE_UNIT_WIDTH 8
27+
#include <pcre2.h>
28+
2629
#include "tscore/ink_assert.h"
2730
#include "tscore/ink_defs.h"
2831
#include "tsutil/Regex.h"
@@ -168,7 +171,7 @@ TEST_CASE("Regex", "[libts][Regex]")
168171
Regex r;
169172
RegexMatches matches;
170173
REQUIRE(r.exec("foo") == false);
171-
REQUIRE(r.exec("foo", matches) == 0);
174+
REQUIRE(r.exec("foo", matches) == PCRE2_ERROR_NULL);
172175
}
173176

174177
// test for recompiling the regular expression

0 commit comments

Comments
 (0)