Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(959)

Unified Diff: base/time_posix.cc

Issue 8771: time overflow (Closed) Base URL: svn://chrome-svn.corp.google.com/chrome/trunk/src/
Patch Set: '' Created 12 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/time_posix.cc
===================================================================
--- base/time_posix.cc (revision 4280)
+++ base/time_posix.cc (working copy)
@@ -10,6 +10,8 @@
#include <sys/time.h>
#include <time.h>
+#include <limits>
+
#include "base/basictypes.h"
#include "base/logging.h"
@@ -58,16 +60,34 @@
seconds = mktime(&timestruct);
else
seconds = timegm(&timestruct);
- DCHECK(seconds >= 0) << "mktime/timegm could not convert from exploded";
- uint64 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond;
+ int64 milliseconds;
+ // Handle overflow.
+ if (seconds == -1 &&
+ (exploded.year < 1969 || exploded.year > 1970)) {
+ // If exploded.year is 1969 or 1970, take -1 as correct, with the
+ // time indicating 1 second prior to the epoch. (1970 is allowed to handle
+ // time zone and DST offsets.) Otherwise, return the most future or past
+ // time representable. Assumes the time_t epoch is 1970-01-01 00:00:00 UTC.
+ if (exploded.year < 1969) {
+ milliseconds = std::numeric_limits<time_t>::min() *
wtc 2008/10/31 20:52:01 Some points to ponder: - What if time_t is uint32
Mark Mentovai 2008/10/31 21:21:01 wtc wrote:
+ kMillisecondsPerSecond;
+ } else {
+ milliseconds = (std::numeric_limits<time_t>::max() *
+ kMillisecondsPerSecond) +
+ kMillisecondsPerSecond - 1;
wtc 2008/10/31 20:52:01 The need to add 999 milliseconds is still not clea
Mark Mentovai 2008/10/31 21:21:01 wtc wrote:
+ }
+ } else {
+ milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond;
+ }
+
return Time(milliseconds * kMicrosecondsPerMillisecond);
}
void Time::Explode(bool is_local, Exploded* exploded) const {
// Time stores times with microsecond resolution, but Exploded only carries
// millisecond resolution, so begin by being lossy.
- uint64 milliseconds = us_ / kMicrosecondsPerMillisecond;
+ int64 milliseconds = us_ / kMicrosecondsPerMillisecond;
time_t seconds = milliseconds / kMillisecondsPerSecond;
struct tm timestruct;
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698