Skip to content

Commit

Permalink
Catch exception and return false in cases where std::regex_match thro…
Browse files Browse the repository at this point in the history
…ws. (#10861)

Signed-off-by: Antonio Vicente <avd@google.com>
  • Loading branch information
antoniovicente authored Apr 21, 2020
1 parent ed55058 commit b268183
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion source/common/common/regex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ class CompiledStdMatcher : public CompiledMatcher {

// CompiledMatcher
bool match(absl::string_view value) const override {
return std::regex_match(value.begin(), value.end(), regex_);
try {
return std::regex_match(value.begin(), value.end(), regex_);
} catch (const std::regex_error& e) {
return false;
}
}

// CompiledMatcher
Expand Down
12 changes: 12 additions & 0 deletions test/common/common/regex_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ TEST(Utility, ParseStdRegex) {
EXPECT_THROW_WITH_REGEX(Utility::parseStdRegex("(+invalid)"), EnvoyException,
"Invalid regex '\\(\\+invalid\\)': .+");

EXPECT_THROW_WITH_REGEX(Utility::parseStdRegexAsCompiledMatcher("(+invalid)"), EnvoyException,
"Invalid regex '\\(\\+invalid\\)': .+");

{
std::regex regex = Utility::parseStdRegex("x*");
EXPECT_NE(0, regex.flags() & std::regex::optimize);
Expand All @@ -25,6 +28,15 @@ TEST(Utility, ParseStdRegex) {
EXPECT_NE(0, regex.flags() & std::regex::icase);
EXPECT_EQ(0, regex.flags() & std::regex::optimize);
}

{
// Regression test to cover high-complexity regular expressions that throw on std::regex_match.
// Note that not all std::regex_match implementations will throw when matching against the
// expression below, but at least clang 9.0.0 under linux does.
auto matcher = Utility::parseStdRegexAsCompiledMatcher(
"|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||");
EXPECT_FALSE(matcher->match("0"));
}
}

TEST(Utility, ParseRegex) {
Expand Down

0 comments on commit b268183

Please sign in to comment.