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

Unified Diff: base/time/time_mac.cc

Issue 2405453002: Fix Integer-overflow in base::Time::FromExploded. (Closed)
Patch Set: rebased and fixed net unittest Created 4 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 | « base/time/time.h ('k') | base/time/time_posix.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/time/time_mac.cc
diff --git a/base/time/time_mac.cc b/base/time/time_mac.cc
index 5803acd35117c175fc43067ac59ce880ee0197b2..c75423df9c2229e50fd102efe760191b950a4c71 100644
--- a/base/time/time_mac.cc
+++ b/base/time/time_mac.cc
@@ -190,9 +190,18 @@ bool Time::FromExploded(bool is_local, const Exploded& exploded, Time* time) {
exploded.millisecond);
CFAbsoluteTime seconds = absolute_time + kCFAbsoluteTimeIntervalSince1970;
- base::Time converted_time =
- Time(static_cast<int64_t>(seconds * kMicrosecondsPerSecond) +
- kWindowsEpochDeltaMicroseconds);
+ // CFAbsolutTime is typedef of double. Convert seconds to
+ // microseconds and then cast to int64. If
+ // it cannot be suited to int64, then fail to avoid overflows.
+ double microseconds =
+ (seconds * kMicrosecondsPerSecond) + kWindowsEpochDeltaMicroseconds;
+ if (microseconds > std::numeric_limits<int64_t>::max() ||
+ microseconds < std::numeric_limits<int64_t>::min()) {
+ *time = Time(0);
+ return false;
+ }
+
+ base::Time converted_time = Time(static_cast<int64_t>(microseconds));
// If |exploded.day_of_month| is set to 31
// on a 28-30 day month, it will return the first day of the next month.
« no previous file with comments | « base/time/time.h ('k') | base/time/time_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698