summaryrefslogtreecommitdiff
path: root/src/mongo/bson
diff options
context:
space:
mode:
authorRui Liu <rui.liu@mongodb.com>2021-11-30 10:37:15 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-11-30 11:23:58 +0000
commitab9802213c7afc0f88d497dee44c83ec6466435c (patch)
tree6557a31bfe1fcfe3ceb282ab060366632ca49d12 /src/mongo/bson
parentf84899b472bef1dbb1fd47a069f1dd77503d8c6d (diff)
downloadmongo-ab9802213c7afc0f88d497dee44c83ec6466435c.tar.gz
SERVER-61566 Fix and extract coercion to 32-bit int logic in expression parsing
Diffstat (limited to 'src/mongo/bson')
-rw-r--r--src/mongo/bson/bsonelement.cpp18
-rw-r--r--src/mongo/bson/bsonelement.h13
2 files changed, 29 insertions, 2 deletions
diff --git a/src/mongo/bson/bsonelement.cpp b/src/mongo/bson/bsonelement.cpp
index d93f928861c..e02dfd07f33 100644
--- a/src/mongo/bson/bsonelement.cpp
+++ b/src/mongo/bson/bsonelement.cpp
@@ -515,7 +515,8 @@ StatusWith<long long> BSONElement::parseIntegerElementToNonNegativeLong() const
if (number.getValue() < 0) {
return Status(ErrorCodes::FailedToParse,
- str::stream() << "Expected a positive number in: " << toString(true, true));
+ str::stream()
+ << "Expected a non-negative number in: " << toString(true, true));
}
return number;
@@ -585,6 +586,21 @@ StatusWith<int> BSONElement::parseIntegerElementToInt() const {
return static_cast<int>(valueLong);
}
+StatusWith<int> BSONElement::parseIntegerElementToNonNegativeInt() const {
+ auto number = parseIntegerElementToInt();
+ if (!number.isOK()) {
+ return number;
+ }
+
+ if (number.getValue() < 0) {
+ return Status(ErrorCodes::FailedToParse,
+ str::stream()
+ << "Expected a non-negative number in: " << toString(true, true));
+ }
+
+ return number;
+}
+
BSONObj BSONElement::embeddedObjectUserCheck() const {
if (MONGO_likely(isABSONObj()))
return BSONObj(value(), BSONObj::LargeSizeTrait{});
diff --git a/src/mongo/bson/bsonelement.h b/src/mongo/bson/bsonelement.h
index 11a8ad6994a..c99623c7e80 100644
--- a/src/mongo/bson/bsonelement.h
+++ b/src/mongo/bson/bsonelement.h
@@ -412,7 +412,7 @@ public:
long long exactNumberLong() const;
/**
- * Parses a BSONElement of any numeric type into a positive long long, failing if the value
+ * Parses a BSONElement of any numeric type into a non-negative long long, failing if the value
* is any of the following:
*
* - NaN.
@@ -433,6 +433,17 @@ public:
StatusWith<long long> parseIntegerElementToLong() const;
/**
+ * Parses a BSONElement of any numeric type into a non-negative int, failing if the value
+ * is any of the following:
+ *
+ * - NaN
+ * - Negative
+ * - a non-integral number
+ * - too large in the positive or negative direction to fit in an int
+ */
+ StatusWith<int> parseIntegerElementToNonNegativeInt() const;
+
+ /**
* Parses a BSONElement of any numeric type into an integer, failing if the value is:
*
* - NaN