diff options
author | Shaun Verch <shaun.verch@mongodb.com> | 2014-05-13 17:01:04 -0400 |
---|---|---|
committer | Shaun Verch <shaun.verch@mongodb.com> | 2014-05-16 14:58:18 -0400 |
commit | bf5f075b3b73c242042be8fd2c0b9b60af5f0089 (patch) | |
tree | d29448d3df2762dfee77783386c8a8c564537a79 /src | |
parent | 50045df21469d45216c5fb8899d68818fcc7f38a (diff) | |
download | mongo-bf5f075b3b73c242042be8fd2c0b9b60af5f0089.tar.gz |
SERVER-13760 Do not call dateToISOString if date is not formatable
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/jsobj.cpp | 16 | ||||
-rw-r--r-- | src/mongo/dbtests/jsontests.cpp | 8 | ||||
-rw-r--r-- | src/mongo/tools/export.cpp | 5 | ||||
-rw-r--r-- | src/mongo/util/time_support.cpp | 17 | ||||
-rw-r--r-- | src/mongo/util/time_support.h | 2 | ||||
-rw-r--r-- | src/mongo/util/time_support_test.cpp | 4 |
6 files changed, 49 insertions, 3 deletions
diff --git a/src/mongo/db/jsobj.cpp b/src/mongo/db/jsobj.cpp index 02a8ebd6549..27472ce6826 100644 --- a/src/mongo/db/jsobj.cpp +++ b/src/mongo/db/jsobj.cpp @@ -211,7 +211,13 @@ namespace mongo { if (format == Strict) { Date_t d = date(); s << "{ \"$date\" : "; - if (static_cast<long long>(d.millis) < 0) { + // The two cases in which we cannot convert Date_t::millis to an ISO Date string are + // when the date is too large to format (SERVER-13760), and when the date is before + // the epoch (SERVER-11273). Since Date_t internally stores millis as an unsigned + // long long, despite the fact that it is logically signed (SERVER-8573), this check + // handles both the case where Date_t::millis is too large, and the case where + // Date_t::millis is negative (before the epoch). + if (d.millis > Date_t::maxFormatableDate) { s << "{ \"$numberLong\" : \"" << static_cast<long long>(d.millis) << "\" }"; } else { @@ -223,7 +229,13 @@ namespace mongo { s << "Date( "; if (pretty) { Date_t d = date(); - if (static_cast<long long>(d.millis) < 0) { + // The two cases in which we cannot convert Date_t::millis to an ISO Date string + // are when the date is too large to format (SERVER-13760), and when the date is + // before the epoch (SERVER-11273). Since Date_t internally stores millis as an + // unsigned long long, despite the fact that it is logically signed + // (SERVER-8573), this check handles both the case where Date_t::millis is too + // large, and the case where Date_t::millis is negative (before the epoch). + if (d.millis > Date_t::maxFormatableDate) { // FIXME: This is not parseable by the shell, since it may not fit in a // float s << d.millis; diff --git a/src/mongo/dbtests/jsontests.cpp b/src/mongo/dbtests/jsontests.cpp index c5d63739207..a8e17ca4ba5 100644 --- a/src/mongo/dbtests/jsontests.cpp +++ b/src/mongo/dbtests/jsontests.cpp @@ -418,6 +418,14 @@ namespace JsonTests { built.jsonString( Strict ) ); ASSERT_EQUALS( "{ \"a\" : Date( 0 ) }", built.jsonString( TenGen ) ); ASSERT_EQUALS( "{ \"a\" : Date( 0 ) }", built.jsonString( JS ) ); + + // Test dates above our maximum formattable date. See SERVER-13760. + BSONObjBuilder b2; + b2.appendDate("a", 32535262800000ULL); + BSONObj built2 = b2.done(); + ASSERT_EQUALS( + "{ \"a\" : { \"$date\" : { \"$numberLong\" : \"32535262800000\" } } }", + built2.jsonString( Strict ) ); } private: diff --git a/src/mongo/tools/export.cpp b/src/mongo/tools/export.cpp index 5e9bca22432..cc7c362889f 100644 --- a/src/mongo/tools/export.cpp +++ b/src/mongo/tools/export.cpp @@ -92,7 +92,10 @@ public: case jstOID: return "ObjectID(" + object.OID().toString() + ")"; // OIDs are always 24 bytes case Date: - return dateToISOStringUTC(object.Date()); + // We need to check if we can actually format this date. See SERVER-13760. + return object.Date().millis > Date_t::maxFormatableDate ? + csvEscape(object.jsonString(Strict, false)) : + dateToISOStringUTC(object.Date()); case Timestamp: return csvEscape(object.jsonString(Strict, false)); case RegEx: diff --git a/src/mongo/util/time_support.cpp b/src/mongo/util/time_support.cpp index afe9010a31f..4f163061655 100644 --- a/src/mongo/util/time_support.cpp +++ b/src/mongo/util/time_support.cpp @@ -42,6 +42,22 @@ namespace mongo { +namespace { + const bool isTimeTSmall = + (sizeof(time_t) == sizeof(int32_t)) && std::numeric_limits<time_t>::is_signed; + + // Some of the library functions we use do not support dates past this. See SERVER-13760. + Date_t getMaxFormatableDate() { + StatusWith<Date_t> mfd = dateFromISOString(isTimeTSmall ? + "2038-01-19T03:14:07Z" : + "3000-12-31T23:59:59Z"); + fassert(17486, mfd.getStatus()); + return mfd.getValue(); + } +} // namespace + + const Date_t Date_t::maxFormatableDate = getMaxFormatableDate(); + // jsTime_virtual_skew is just for testing. a test command manipulates it. long long jsTime_virtual_skew = 0; boost::thread_specific_ptr<long long> jsTime_virtual_thread_skew; @@ -118,6 +134,7 @@ namespace { }; void _dateToISOString(Date_t date, bool local, DateStringBuffer* result) { + invariant(date.millis <= Date_t::maxFormatableDate); static const int bufSize = DateStringBuffer::dataCapacity; char* const buf = result->data; struct tm t; diff --git a/src/mongo/util/time_support.h b/src/mongo/util/time_support.h index 61e6a1b02ff..9ddc39307d7 100644 --- a/src/mongo/util/time_support.h +++ b/src/mongo/util/time_support.h @@ -45,6 +45,8 @@ namespace mongo { int64_t asInt64() const { return static_cast<int64_t>(millis); } + + static const Date_t maxFormatableDate; }; // uses ISO 8601 dates without trailing Z diff --git a/src/mongo/util/time_support_test.cpp b/src/mongo/util/time_support_test.cpp index d22195c3a0e..66f763799b9 100644 --- a/src/mongo/util/time_support_test.cpp +++ b/src/mongo/util/time_support_test.cpp @@ -168,6 +168,10 @@ namespace { swull = dateFromISOString("2058-02-20T18:29:11.100Z"); ASSERT_OK(swull.getStatus()); ASSERT_EQUALS(swull.getValue(), 2781455351100ULL); + + swull = dateFromISOString("3001-01-01T08:00:00.000Z"); + ASSERT_OK(swull.getStatus()); + ASSERT_EQUALS(swull.getValue(), 32535244800000ULL); } swull = dateFromISOString("2013-02-20T18:29:11.100Z"); |