summaryrefslogtreecommitdiff
path: root/src/mongo/bson
diff options
context:
space:
mode:
authorVincent Do <vincent.do@mongodb.com>2016-06-10 15:49:13 -0400
committerVincent Do <vincent.do@10gen.com>2016-06-13 11:03:16 -0400
commitda9f93e1695852b01d4bbcfad247669725c9353a (patch)
tree1c6f4db42dc38b5e7560591cfc871de1e20e449f /src/mongo/bson
parentfd77e162bd656f245e4735385a8077472142c08b (diff)
downloadmongo-da9f93e1695852b01d4bbcfad247669725c9353a.tar.gz
SERVER-24525 Fix BSON encoding and decoding for decimal
Diffstat (limited to 'src/mongo/bson')
-rw-r--r--src/mongo/bson/bsonelement.h4
-rw-r--r--src/mongo/bson/bsonobjbuilder.h1
-rw-r--r--src/mongo/bson/util/builder.h13
3 files changed, 13 insertions, 5 deletions
diff --git a/src/mongo/bson/bsonelement.h b/src/mongo/bson/bsonelement.h
index 876e4a40050..ad8429da9be 100644
--- a/src/mongo/bson/bsonelement.h
+++ b/src/mongo/bson/bsonelement.h
@@ -304,7 +304,9 @@ public:
/** Return decimal128 value for this field. MUST be NumberDecimal type. */
Decimal128 _numberDecimal() const {
- return Decimal128(ConstDataView(value()).read<LittleEndian<Decimal128::Value>>());
+ uint64_t low = ConstDataView(value()).read<LittleEndian<long long>>();
+ uint64_t high = ConstDataView(value() + sizeof(long long)).read<LittleEndian<long long>>();
+ return Decimal128(Decimal128::Value({low, high}));
}
/** Return long long value for this field. MUST be NumberLong type. */
diff --git a/src/mongo/bson/bsonobjbuilder.h b/src/mongo/bson/bsonobjbuilder.h
index 74593287942..10b8a93b80e 100644
--- a/src/mongo/bson/bsonobjbuilder.h
+++ b/src/mongo/bson/bsonobjbuilder.h
@@ -260,6 +260,7 @@ public:
BSONObjBuilder& append(StringData fieldName, Decimal128 n) {
_b.appendNum(static_cast<char>(NumberDecimal));
_b.appendStr(fieldName);
+ // Make sure we write data in a Little Endian conforming manner
_b.appendNum(n);
return *this;
}
diff --git a/src/mongo/bson/util/builder.h b/src/mongo/bson/util/builder.h
index 924d96e7f8b..53ad7d25ebe 100644
--- a/src/mongo/bson/util/builder.h
+++ b/src/mongo/bson/util/builder.h
@@ -212,6 +212,15 @@ public:
appendNumImpl(j);
}
+ void appendNum(Decimal128 j) {
+ BOOST_STATIC_ASSERT(sizeof(Decimal128::Value) == 16);
+ Decimal128::Value value = j.getValue();
+ long long low = value.low64;
+ long long high = value.high64;
+ appendNumImpl(low);
+ appendNumImpl(high);
+ }
+
template <typename Int64_t,
typename = stdx::enable_if_t<std::is_same<Int64_t, int64_t>::value &&
!std::is_same<int64_t, long long>::value>>
@@ -222,10 +231,6 @@ public:
void appendNum(unsigned long long j) {
appendNumImpl(j);
}
- void appendNum(Decimal128 j) {
- BOOST_STATIC_ASSERT(sizeof(Decimal128::Value) == 16);
- appendNumImpl(j.getValue());
- }
void appendBuf(const void* src, size_t len) {
if (len)