diff options
author | Eric Milkie <milkie@10gen.com> | 2015-04-07 16:42:23 -0400 |
---|---|---|
committer | Eric Milkie <milkie@10gen.com> | 2015-04-08 16:27:14 -0400 |
commit | e87716a9286b6aa6f63a513012e55f6e42f634a2 (patch) | |
tree | 06e906a531f9699bc6e5bee143339eee4bf8841a /src/mongo/bson/timestamp.cpp | |
parent | 655f6f61888035798519a5866dec9f9d76f7c224 (diff) | |
download | mongo-e87716a9286b6aa6f63a513012e55f6e42f634a2.tar.gz |
SERVER-15047 Remove undefined behavior from Timestamp
Diffstat (limited to 'src/mongo/bson/timestamp.cpp')
-rw-r--r-- | src/mongo/bson/timestamp.cpp | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/src/mongo/bson/timestamp.cpp b/src/mongo/bson/timestamp.cpp index cfd43fe8fe2..2aea7306e37 100644 --- a/src/mongo/bson/timestamp.cpp +++ b/src/mongo/bson/timestamp.cpp @@ -25,14 +25,15 @@ * then also delete it in the license file. */ -#include "mongo/bson/bsontypes.h" #include "mongo/bson/timestamp.h" +#include <cstring> #include <ctime> #include <iostream> #include <limits> #include <sstream> +#include "mongo/bson/bsontypes.h" #include "mongo/platform/cstdint.h" #include "mongo/util/time_support.h" @@ -45,29 +46,36 @@ namespace mongo { } void Timestamp::append(BufBuilder& builder, const StringData& fieldName) const { - // No endian conversions needed, since we store in-memory representation - // in little endian format, regardless of target endian. - builder.appendNum( static_cast<char>(bsonTimestamp) ); - builder.appendStr( fieldName ); - builder.appendNum( asULL() ); + // No endian conversions needed, since we store in-memory representation + // in little endian format, regardless of target endian. + builder.appendNum( static_cast<char>(bsonTimestamp) ); + builder.appendStr( fieldName ); + builder.appendBuf(&_data, sizeof(_data)); + } + + const void* Timestamp::readFrom(const void* bytes) { + // No endian conversions needed, since we store in-memory representation + // in little endian format, regardless of target endian. + std::memcpy(&_data, bytes, sizeof(_data)); + return reinterpret_cast<const char*>(bytes) + sizeof(_data); } std::string Timestamp::toStringLong() const { std::stringstream ss; - ss << time_t_to_String_short(secs) << ' '; - ss << std::hex << secs << ':' << i; + ss << time_t_to_String_short(getSecs()) << ' '; + ss << std::hex << getSecs() << ':' << getInc(); return ss.str(); } std::string Timestamp::toStringPretty() const { std::stringstream ss; - ss << time_t_to_String_short(secs) << ':' << std::hex << i; + ss << time_t_to_String_short(getSecs()) << ':' << std::hex << getInc(); return ss.str(); } std::string Timestamp::toString() const { std::stringstream ss; - ss << std::hex << secs << ':' << i; + ss << std::hex << getSecs() << ':' << getInc(); return ss.str(); } |