diff options
author | James Wahlin <james@mongodb.com> | 2019-05-31 13:30:03 -0400 |
---|---|---|
committer | James Wahlin <james@mongodb.com> | 2019-05-31 16:54:23 -0400 |
commit | 8bd487a79af7c4d8eac6c6cfd78c60e8a99742ec (patch) | |
tree | 55dcaaf8d57da1087e2d936ea91ce8c2e95e15a6 /src/mongo/db/matcher | |
parent | a587fc4fe1e0c051b8f534d6c2efad35314a899c (diff) | |
download | mongo-8bd487a79af7c4d8eac6c6cfd78c60e8a99742ec.tar.gz |
SERVER-41432 Add ExprMatchExpressionMatchesReturnsFalseOnException failpoint
Diffstat (limited to 'src/mongo/db/matcher')
-rw-r--r-- | src/mongo/db/matcher/expression_expr.cpp | 17 | ||||
-rw-r--r-- | src/mongo/db/matcher/expression_expr_test.cpp | 9 |
2 files changed, 24 insertions, 2 deletions
diff --git a/src/mongo/db/matcher/expression_expr.cpp b/src/mongo/db/matcher/expression_expr.cpp index 39d701bdb1a..de1c1f956eb 100644 --- a/src/mongo/db/matcher/expression_expr.cpp +++ b/src/mongo/db/matcher/expression_expr.cpp @@ -31,7 +31,12 @@ #include "mongo/db/matcher/expression_expr.h" +#include "mongo/util/fail_point_service.h" + namespace mongo { + +MONGO_FAIL_POINT_DEFINE(ExprMatchExpressionMatchesReturnsFalseOnException); + ExprMatchExpression::ExprMatchExpression(boost::intrusive_ptr<Expression> expr, const boost::intrusive_ptr<ExpressionContext>& expCtx) : MatchExpression(MatchType::EXPRESSION), _expCtx(expCtx), _expression(expr) {} @@ -48,8 +53,16 @@ bool ExprMatchExpression::matches(const MatchableDocument* doc, MatchDetails* de } Document document(doc->toBSON()); - auto value = _expression->evaluate(document); - return value.coerceToBool(); + try { + auto value = _expression->evaluate(document); + return value.coerceToBool(); + } catch (const DBException&) { + if (MONGO_FAIL_POINT(ExprMatchExpressionMatchesReturnsFalseOnException)) { + return false; + } + + throw; + } } void ExprMatchExpression::serialize(BSONObjBuilder* out) const { diff --git a/src/mongo/db/matcher/expression_expr_test.cpp b/src/mongo/db/matcher/expression_expr_test.cpp index 7473dfa1a0c..ce97e822b37 100644 --- a/src/mongo/db/matcher/expression_expr_test.cpp +++ b/src/mongo/db/matcher/expression_expr_test.cpp @@ -590,6 +590,15 @@ TEST_F(ExprMatchTest, FailGracefullyOnInvalidExpression) { 17041); } +TEST_F(ExprMatchTest, ReturnsFalseInsteadOfErrorWithFailpointSet) { + createMatcher(fromjson("{$expr: {$divide: [10, '$divisor']}}")); + ASSERT_THROWS_CODE(matches(BSON("divisor" << 0)), AssertionException, 16608); + + FailPointEnableBlock scopedFailpoint("ExprMatchExpressionMatchesReturnsFalseOnException"); + createMatcher(fromjson("{$expr: {$divide: [10, '$divisor']}}")); + ASSERT_FALSE(matches(BSON("divisor" << 0))); +} + TEST(ExprMatchTest, IdenticalPostOptimizedExpressionsAreEquivalent) { BSONObj expression = BSON("$expr" << BSON("$multiply" << BSON_ARRAY(2 << 2))); BSONObj expressionEquiv = BSON("$expr" << BSON("$const" << 4)); |