This is an automated email from the ASF dual-hosted git repository.
bneradt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push:
new c9fb43eac2 ControlMatcher: error versus NOMATCH checking (#12551)
c9fb43eac2 is described below
commit c9fb43eac265779b820b60732f3f09809599a5bf
Author: Brian Neradt <[email protected]>
AuthorDate: Fri Oct 10 10:44:57 2025 -0500
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.
---
src/proxy/ControlMatcher.cc | 21 +++++++++++++--------
src/tsutil/Regex.cc | 4 ++--
src/tsutil/unit_tests/test_Regex.cc | 5 ++++-
3 files changed, 19 insertions(+), 11 deletions(-)
diff --git a/src/proxy/ControlMatcher.cc b/src/proxy/ControlMatcher.cc
index 234a45e2ee..de982f8c8d 100644
--- a/src/proxy/ControlMatcher.cc
+++ b/src/proxy/ControlMatcher.cc
@@ -476,13 +476,15 @@ RegexMatcher<Data, MatchResult>::Match(RequestData
*rdata, MatchResult *result)
// The function unescapifyStr() is already called in
// HttpRequestData::get_string(); therefore, no need to call again here.
for (int i = 0; i < num_el; i++) {
- if (regex_array[i].exec(url_str) == true) {
+ RegexMatches matches;
+ int r = regex_array[i].exec(url_str, matches);
+ if (r >= 0) {
Dbg(dbg_ctl_matcher, "%s Matched %s with regex at line %d",
matcher_name, url_str, data_array[i].line_num);
data_array[i].UpdateMatch(result, rdata);
- } else {
+ } else if (r != PCRE2_ERROR_NOMATCH) {
// An error has occurred
- Warning("Error matching regex at line %d.", data_array[i].line_num);
- } // else it's -1 which means no match was found.
+ Warning("Error matching regex for url: %s:%d (%d)", file_name ?
file_name : "unknown", data_array[i].line_num, r);
+ } // else: PCRE2_ERROR_NOMATCH
}
ats_free(url_str);
}
@@ -521,14 +523,17 @@ HostRegexMatcher<Data, MatchResult>::Match(RequestData
*rdata, MatchResult *resu
url_str = "";
}
for (int i = 0; i < num_el; i++) {
- if (this->regex_array[i].exec(url_str) == true) {
+ RegexMatches matches;
+ int r = this->regex_array[i].exec(url_str, matches);
+ if (r >= 0) {
Dbg(dbg_ctl_matcher, "%s Matched %s with regex at line %d",
const_cast<char *>(this->matcher_name), url_str,
this->data_array[i].line_num);
this->data_array[i].UpdateMatch(result, rdata);
- } else {
+ } else if (r != PCRE2_ERROR_NOMATCH) {
// An error has occurred
- Warning("error matching regex at line %d", this->data_array[i].line_num);
- }
+ Warning("Error matching regex for host: %s:%d (%d)", this->file_name ?
this->file_name : "unknown",
+ this->data_array[i].line_num, r);
+ } // else: PCRE2_ERROR_NOMATCH
}
}
diff --git a/src/tsutil/Regex.cc b/src/tsutil/Regex.cc
index 03e9cba6d3..32604fb6ae 100644
--- a/src/tsutil/Regex.cc
+++ b/src/tsutil/Regex.cc
@@ -312,7 +312,7 @@ Regex::exec(std::string_view subject, uint32_t flags) const
RegexMatches matches;
int count = this->exec(subject, matches, flags);
- return count > 0;
+ return count >= 0;
}
//----------------------------------------------------------------------------
@@ -330,7 +330,7 @@ Regex::exec(std::string_view subject, RegexMatches
&matches, uint32_t flags) con
// check if there is a compiled regex
if (code == nullptr) {
- return 0;
+ return PCRE2_ERROR_NULL;
}
int count = pcre2_match(code, reinterpret_cast<PCRE2_SPTR>(subject.data()),
subject.size(), 0, flags,
RegexMatches::_MatchData::get(matches._match_data),
RegexContext::get_instance()->get_match_context());
diff --git a/src/tsutil/unit_tests/test_Regex.cc
b/src/tsutil/unit_tests/test_Regex.cc
index ebc0f1a764..c2021322bf 100644
--- a/src/tsutil/unit_tests/test_Regex.cc
+++ b/src/tsutil/unit_tests/test_Regex.cc
@@ -23,6 +23,9 @@
#include <string_view>
#include <vector>
+#define PCRE2_CODE_UNIT_WIDTH 8
+#include <pcre2.h>
+
#include "tscore/ink_assert.h"
#include "tscore/ink_defs.h"
#include "tsutil/Regex.h"
@@ -168,7 +171,7 @@ TEST_CASE("Regex", "[libts][Regex]")
Regex r;
RegexMatches matches;
REQUIRE(r.exec("foo") == false);
- REQUIRE(r.exec("foo", matches) == 0);
+ REQUIRE(r.exec("foo", matches) == PCRE2_ERROR_NULL);
}
// test for recompiling the regular expression