summaryrefslogtreecommitdiff
path: root/src/mongo/db/matcher
diff options
context:
space:
mode:
authorKyle Suarez <kyle.suarez@mongodb.com>2018-06-19 17:21:56 -0400
committerKyle Suarez <kyle.suarez@mongodb.com>2018-06-20 15:43:44 -0400
commite9a6f62b3053733354cabb6d9488d0d4dcc6c424 (patch)
tree2845935cf4ec0c2d031f7b2b3bec92b545f8a16b /src/mongo/db/matcher
parent286fdc12c0d37f5c614875eae9ab541638103b10 (diff)
downloadmongo-e9a6f62b3053733354cabb6d9488d0d4dcc6c424.tar.gz
Revert "SERVER-30986 Prevent invalid regex in RegexMatchExpressions"
There exist PCRE regexes whose error strings are nonempty but can still be used for matching. This restores the functionality of the (*UCP) option for SERVER-34933. This reverts commit 1531b7b7280dd37d5f7ffd49171a65305ad442ba. Conflicts: src/mongo/db/matcher/expression_leaf.cpp src/mongo/db/matcher/expression_leaf.h src/mongo/db/matcher/expression_leaf_test.cpp
Diffstat (limited to 'src/mongo/db/matcher')
-rw-r--r--src/mongo/db/matcher/expression_leaf.cpp9
-rw-r--r--src/mongo/db/matcher/expression_leaf.h6
-rw-r--r--src/mongo/db/matcher/expression_leaf_test.cpp6
3 files changed, 11 insertions, 10 deletions
diff --git a/src/mongo/db/matcher/expression_leaf.cpp b/src/mongo/db/matcher/expression_leaf.cpp
index c2485fa128a..1f6a4df3f7d 100644
--- a/src/mongo/db/matcher/expression_leaf.cpp
+++ b/src/mongo/db/matcher/expression_leaf.cpp
@@ -196,6 +196,8 @@ inline pcrecpp::RE_Options flags2options(const char* flags) {
return options;
}
+constexpr size_t RegexMatchExpression::kMaxPatternSize;
+
RegexMatchExpression::RegexMatchExpression(StringData path, const BSONElement& e)
: LeafMatchExpression(REGEX, path),
_regex(e.regex()),
@@ -214,6 +216,9 @@ 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);
@@ -221,10 +226,6 @@ void RegexMatchExpression::_init() {
uassert(ErrorCodes::BadValue,
"Regular expression options string cannot contain an embedded null byte",
_flags.find('\0') == std::string::npos);
-
- uassert(ErrorCodes::BadValue,
- 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 90c2d9cf0e5..2d33adcc883 100644
--- a/src/mongo/db/matcher/expression_leaf.h
+++ b/src/mongo/db/matcher/expression_leaf.h
@@ -281,8 +281,14 @@ public:
class RegexMatchExpression : public LeafMatchExpression {
public:
+ /**
+ * The maximum length of regex patterns.
+ */
+ static constexpr size_t kMaxPatternSize = 32764;
+
RegexMatchExpression(StringData path, const BSONElement& e);
RegexMatchExpression(StringData path, StringData regex, StringData options);
+
~RegexMatchExpression();
virtual std::unique_ptr<MatchExpression> shallowClone() const {
diff --git a/src/mongo/db/matcher/expression_leaf_test.cpp b/src/mongo/db/matcher/expression_leaf_test.cpp
index dde4439d517..87ac6861f7f 100644
--- a/src/mongo/db/matcher/expression_leaf_test.cpp
+++ b/src/mongo/db/matcher/expression_leaf_test.cpp
@@ -896,12 +896,6 @@ TEST(RegexMatchExpression, RegexOptionsStringCannotContainEmbeddedNullByte) {
}
}
-TEST(RegexMatchExpression, RegexCannotBeInvalid) {
- const auto invalid = "["_sd;
- ASSERT_THROWS_CODE(
- RegexMatchExpression regex("path", invalid, ""), AssertionException, ErrorCodes::BadValue);
-}
-
TEST(ModMatchExpression, MatchesElement) {
BSONObj match = BSON("a" << 1);
BSONObj largerMatch = BSON("a" << 4.0);