diff options
author | Reo Kimura <reo.kimura@mongodb.com> | 2021-09-22 18:46:21 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-09-22 19:32:53 +0000 |
commit | b16a1efdc1e16dcd75151641d2d6dd9f2619ef8b (patch) | |
tree | 58287f58bb920124bc2ae0300282f8ae6245d897 | |
parent | ea575e9768e5df64d3855b1392abb180c734387f (diff) | |
download | mongo-b16a1efdc1e16dcd75151641d2d6dd9f2619ef8b.tar.gz |
SERVER-48076 Account for rounding of 64 bit type max in conversion to…
-rw-r--r-- | src/mongo/bson/bsonelement.h | 8 |
1 files changed, 3 insertions, 5 deletions
diff --git a/src/mongo/bson/bsonelement.h b/src/mongo/bson/bsonelement.h index 3502ff8a7f6..e4485d19233 100644 --- a/src/mongo/bson/bsonelement.h +++ b/src/mongo/bson/bsonelement.h @@ -1056,11 +1056,9 @@ Status BSONElement::tryCoerce(T* out) const { if (!std::isfinite(d)) { return {ErrorCodes::BadValue, "Unable to coerce NaN/Inf to integral type"}; } - // TODO(SERVER-48076): Revisit the use of the > operator for the T::max() comparison in - // the case when T is a 64 bit type. When T is a 64 bit type, this comparison should - // be >=, as the conversion rounds the the max to 2**63 which is double that cannot be - // converted. - if ((d > static_cast<double>(std::numeric_limits<T>::max())) || + bool sameMax = std::numeric_limits<T>::max() == std::numeric_limits<long long>::max(); + if ((!sameMax && d > static_cast<double>(std::numeric_limits<T>::max())) || + (sameMax && d >= static_cast<double>(std::numeric_limits<T>::max())) || (d < std::numeric_limits<T>::lowest())) { return {ErrorCodes::BadValue, "Out of bounds coercing to integral value"}; } |