From b16a1efdc1e16dcd75151641d2d6dd9f2619ef8b Mon Sep 17 00:00:00 2001 From: Reo Kimura Date: Wed, 22 Sep 2021 18:46:21 +0000 Subject: =?UTF-8?q?SERVER-48076=20Account=20for=20rounding=20of=2064=20bit?= =?UTF-8?q?=20type=20max=20in=20conversion=20to=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/mongo/bson/bsonelement.h | 8 +++----- 1 file 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(std::numeric_limits::max())) || + bool sameMax = std::numeric_limits::max() == std::numeric_limits::max(); + if ((!sameMax && d > static_cast(std::numeric_limits::max())) || + (sameMax && d >= static_cast(std::numeric_limits::max())) || (d < std::numeric_limits::lowest())) { return {ErrorCodes::BadValue, "Out of bounds coercing to integral value"}; } -- cgit v1.2.1