diff options
author | Vincent Do <do.vincent@live.com> | 2016-05-19 14:41:49 -0400 |
---|---|---|
committer | Vincent Do <vincent.do@10gen.com> | 2016-05-24 15:24:58 -0400 |
commit | 6369be976c5b3188e2bbafd9d7513f6808ac5595 (patch) | |
tree | 9110c67e693cb378d386d70433cffd9e505a47ec /src/mongo/platform/decimal128.cpp | |
parent | 96841731d86f761dd8862ebed64d3f97e86b748c (diff) | |
download | mongo-6369be976c5b3188e2bbafd9d7513f6808ac5595.tar.gz |
SERVER-23702 Fix server crashing on invalid decimal toString invariant check
Diffstat (limited to 'src/mongo/platform/decimal128.cpp')
-rw-r--r-- | src/mongo/platform/decimal128.cpp | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/src/mongo/platform/decimal128.cpp b/src/mongo/platform/decimal128.cpp index 990b18e1569..def39a899e9 100644 --- a/src/mongo/platform/decimal128.cpp +++ b/src/mongo/platform/decimal128.cpp @@ -371,6 +371,17 @@ double Decimal128::toDouble(std::uint32_t* signalingFlags, RoundingMode roundMod } std::string Decimal128::toString() const { + // If the decimal is a variant of NaN (i.e. sNaN, -NaN, +NaN, etc...) or a variant of + // Inf (i.e. +Inf, Inf, -Inf), return either NaN, Infinity, or -Infinity + if (!isFinite()) { + if (this->isEqual(kPositiveInfinity)) { + return "Infinity"; + } else if (this->isEqual(kNegativeInfinity)) { + return "-Infinity"; + } + invariant(isNaN()); + return "NaN"; + } BID_UINT128 dec128 = decimal128ToLibraryType(_value); char decimalCharRepresentation[1 /* mantissa sign */ + 34 /* mantissa */ + 1 /* scientific E */ + 1 /* exponent sign */ + 4 /* exponent */ + @@ -389,16 +400,7 @@ std::string Decimal128::toString() const { std::string dec128String(decimalCharRepresentation); - // If the string is NaN or Infinity, return either NaN, +Inf, or -Inf std::string::size_type ePos = dec128String.find("E"); - if (ePos == std::string::npos) { - if (dec128String == "-NaN" || dec128String == "+NaN") - return "NaN"; - if (dec128String[0] == '+') - return "Inf"; - invariant(dec128String == "-Inf"); - return dec128String; - } // Calculate the precision and exponent of the number and output it in a readable manner int precision = 0; @@ -483,6 +485,10 @@ bool Decimal128::isInfinite() const { return bid128_isInf(decimal128ToLibraryType(_value)); } +bool Decimal128::isFinite() const { + return bid128_isFinite(decimal128ToLibraryType(_value)); +} + bool Decimal128::isNegative() const { return bid128_isSigned(decimal128ToLibraryType(_value)); } |