From 062c6c13cabb6dc26bed460954982925f2e9f611 Mon Sep 17 00:00:00 2001 From: Jennifer Peshansky Date: Wed, 13 Oct 2021 20:26:26 +0000 Subject: SERVER-60588 Don't attempt to coerce a double product to long (cherry picked from commit d0a023d1cba8782c761890c20752ecf494d22a62) (cherry picked from commit f6c05d4d028fe6712e81396be335fd144124c561) (cherry picked from commit 9aa24cb9aaa12578a81361e64a5e57dbe1c34b1e) (cherry picked from commit 4ea0988ab20a69369b8a8ceaf5015c94f253a289) --- src/mongo/db/pipeline/expression.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/mongo/db/pipeline/expression.cpp b/src/mongo/db/pipeline/expression.cpp index 0aabae477c7..df5dad777b0 100644 --- a/src/mongo/db/pipeline/expression.cpp +++ b/src/mongo/db/pipeline/expression.cpp @@ -2701,11 +2701,17 @@ Value ExpressionMultiply::evaluate(const Document& root, Variables* variables) c decimalProduct = decimalProduct.multiply(val.coerceToDecimal()); } else { doubleProduct *= val.coerceToDouble(); - if (!std::isfinite(val.coerceToDouble()) || - mongoSignedMultiplyOverflow64(longProduct, val.coerceToLong(), &longProduct)) { - // The number is either Infinity or NaN, or the 'longProduct' would have - // overflowed, so we're abandoning it. - productType = NumberDouble; + + if (productType != NumberDouble) { + // If `productType` is not a double, it must be one of the integer types, so we + // attempt to update `longProduct`. + if (!std::isfinite(val.coerceToDouble()) || + mongoSignedMultiplyOverflow64( + longProduct, val.coerceToLong(), &longProduct)) { + // The number is either Infinity or NaN, or the 'longProduct' would have + // overflowed, so we're abandoning it. + productType = NumberDouble; + } } } } else if (val.nullish()) { -- cgit v1.2.1