From b4119be75bb8f31da7e8f28bb4e3f881f3bc5027 Mon Sep 17 00:00:00 2001 From: Kevin Albertson Date: Mon, 9 Jan 2023 19:21:47 +0000 Subject: SERVER-72621 truncate v_prime2 --- src/mongo/crypto/fle_crypto.cpp | 5 +++++ src/mongo/crypto/fle_crypto_test.cpp | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/src/mongo/crypto/fle_crypto.cpp b/src/mongo/crypto/fle_crypto.cpp index c5bda150f5f..70c37d22ce7 100644 --- a/src/mongo/crypto/fle_crypto.cpp +++ b/src/mongo/crypto/fle_crypto.cpp @@ -4004,6 +4004,11 @@ OSTType_Decimal128 getTypeInfoDecimal128(Decimal128 value, // // Returns 3141.0 Decimal128 v_prime2 = v_prime.subtract(min.get()).scale(precision.get()); + // Round the number down again. min may have a fractional value with more decimal places + // than the precision (e.g. .001). Subtracting min may have resulted in v_prime2 with + // a non-zero fraction. v_prime2 is expected to have no fractional value when + // converting to int128. + v_prime2 = v_prime2.round(Decimal128::kRoundTowardZero); invariant(v_prime2.logarithm(Decimal128(2)).isLess(Decimal128(128))); diff --git a/src/mongo/crypto/fle_crypto_test.cpp b/src/mongo/crypto/fle_crypto_test.cpp index 538211edd9f..e655d10cd2b 100644 --- a/src/mongo/crypto/fle_crypto_test.cpp +++ b/src/mongo/crypto/fle_crypto_test.cpp @@ -2768,6 +2768,12 @@ TEST(RangeTest, Decimal128_Bounds_Precision) { ASSERT_EIBB(5E-30, 10E-30, 1E-30, 35, boost::multiprecision::uint128_t("400000")); + // Test a range that requires > 64 bits. + ASSERT_EIBB(5, "18446744073709551616", ".1", 1, 49) + // Test a range that requires > 64 bits. + // min has more places after the decimal than precision. + ASSERT_EIBB(5, "18446744073709551616", ".01", 1, 49) + #undef ASSERT_EIBB #undef ASSERT_EIBB_OVERFLOW } -- cgit v1.2.1