diff options
author | Ian Boros <puppyofkosh@gmail.com> | 2019-02-25 14:38:42 -0500 |
---|---|---|
committer | Ian Boros <puppyofkosh@gmail.com> | 2019-02-28 13:50:38 -0500 |
commit | 5f772b0b1139f4b9bbff098d4bcee59cf2e95cda (patch) | |
tree | 6b47af38db51ef2ca06aaf1e8d6d3250124d1d83 /src | |
parent | 1eadc480a431473e8c975e9d2eccb06ad7831c87 (diff) | |
download | mongo-5f772b0b1139f4b9bbff098d4bcee59cf2e95cda.tar.gz |
SERVER-39697 MatchExpressions regex error at parse time if regex is invalid
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/matcher/expression_leaf.cpp | 8 | ||||
-rw-r--r-- | src/mongo/db/matcher/expression_leaf.h | 1 | ||||
-rw-r--r-- | src/mongo/db/matcher/expression_leaf_test.cpp | 22 |
3 files changed, 17 insertions, 14 deletions
diff --git a/src/mongo/db/matcher/expression_leaf.cpp b/src/mongo/db/matcher/expression_leaf.cpp index 52d19d188c9..1a0c7934495 100644 --- a/src/mongo/db/matcher/expression_leaf.cpp +++ b/src/mongo/db/matcher/expression_leaf.cpp @@ -217,7 +217,6 @@ inline pcrecpp::RE_Options flags2options(const char* flags) { } const std::set<char> RegexMatchExpression::kValidRegexFlags = {'i', 'm', 's', 'x'}; -constexpr size_t RegexMatchExpression::kMaxPatternSize; RegexMatchExpression::RegexMatchExpression(StringData path, const BSONElement& e) : LeafMatchExpression(REGEX, path), @@ -237,9 +236,6 @@ RegexMatchExpression::RegexMatchExpression(StringData path, StringData regex, St } void RegexMatchExpression::_init() { - uassert( - ErrorCodes::BadValue, "Regular expression is too long", _regex.size() <= kMaxPatternSize); - uassert(ErrorCodes::BadValue, "Regular expression cannot contain an embedded null byte", _regex.find('\0') == std::string::npos); @@ -247,6 +243,10 @@ void RegexMatchExpression::_init() { uassert(ErrorCodes::BadValue, "Regular expression options string cannot contain an embedded null byte", _flags.find('\0') == std::string::npos); + + uassert(51091, + str::stream() << "Regular expression is invalid: " << _re->error(), + _re->error().empty()); } RegexMatchExpression::~RegexMatchExpression() {} diff --git a/src/mongo/db/matcher/expression_leaf.h b/src/mongo/db/matcher/expression_leaf.h index 66c25dcf1c8..8da9948044b 100644 --- a/src/mongo/db/matcher/expression_leaf.h +++ b/src/mongo/db/matcher/expression_leaf.h @@ -282,7 +282,6 @@ public: class RegexMatchExpression : public LeafMatchExpression { public: - static constexpr size_t kMaxPatternSize = 32764; static const std::set<char> kValidRegexFlags; RegexMatchExpression(StringData path, const BSONElement& e); diff --git a/src/mongo/db/matcher/expression_leaf_test.cpp b/src/mongo/db/matcher/expression_leaf_test.cpp index 2b8a7dc816a..be0b2aa9445 100644 --- a/src/mongo/db/matcher/expression_leaf_test.cpp +++ b/src/mongo/db/matcher/expression_leaf_test.cpp @@ -716,8 +716,7 @@ TEST(RegexMatchExpression, MatchesElementExact) { TEST(RegexMatchExpression, TooLargePattern) { string tooLargePattern(50 * 1000, 'z'); - ASSERT_THROWS_CODE( - RegexMatchExpression("a", tooLargePattern, ""), AssertionException, ErrorCodes::BadValue); + ASSERT_THROWS_CODE(RegexMatchExpression("a", tooLargePattern, ""), AssertionException, 51091); } TEST(RegexMatchExpression, MatchesElementSimplePrefix) { @@ -976,15 +975,20 @@ TEST(RegexMatchExpression, RegexOptionsStringCannotContainEmbeddedNullByte) { } } -TEST(RegexMatchExpression, MalformedRegexAcceptedButMatchesNothing) { - RegexMatchExpression regex("a", "[(*ACCEPT)", ""); - ASSERT_FALSE(regex.matchesBSON(BSON("a" - << ""))); - ASSERT_FALSE(regex.matchesBSON(BSON("a" - << "["))); +TEST(RegexMatchExpression, MalformedRegexNotAccepted) { + ASSERT_THROWS_CODE(RegexMatchExpression("a", // path + "[", // regex + "" // options + ), + AssertionException, + 51091); } -TEST(RegexMatchExpression, RegexAcceptsUCPOption) { +TEST(RegexMatchExpression, MalformedRegexWithStartOptionNotAccepted) { + ASSERT_THROWS_CODE(RegexMatchExpression("a", "[(*ACCEPT)", ""), AssertionException, 51091); +} + +TEST(RegexMatchExpression, RegexAcceptsUCPStartOption) { RegexMatchExpression regex("a", "(*UCP)(\\w|\u304C)", ""); ASSERT(regex.matchesBSON(BSON("a" << "k"))); |