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

Side by Side Diff: base/time/time_win.cc

Issue 2405453002: Fix Integer-overflow in base::Time::FromExploded. (Closed)
Patch Set: rebased and fixed net unittest Created 4 years, 1 month 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 5
6 // Windows Timer Primer 6 // Windows Timer Primer
7 // 7 //
8 // A good article: http://www.ddj.com/windows/184416651 8 // A good article: http://www.ddj.com/windows/184416651
9 // A good mozilla bug: http://bugzilla.mozilla.org/show_bug.cgi?id=363258 9 // A good mozilla bug: http://bugzilla.mozilla.org/show_bug.cgi?id=363258
10 // 10 //
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 // Returns the current value of the performance counter. 103 // Returns the current value of the performance counter.
104 uint64_t QPCNowRaw() { 104 uint64_t QPCNowRaw() {
105 LARGE_INTEGER perf_counter_now = {}; 105 LARGE_INTEGER perf_counter_now = {};
106 // According to the MSDN documentation for QueryPerformanceCounter(), this 106 // According to the MSDN documentation for QueryPerformanceCounter(), this
107 // will never fail on systems that run XP or later. 107 // will never fail on systems that run XP or later.
108 // https://msdn.microsoft.com/library/windows/desktop/ms644904.aspx 108 // https://msdn.microsoft.com/library/windows/desktop/ms644904.aspx
109 ::QueryPerformanceCounter(&perf_counter_now); 109 ::QueryPerformanceCounter(&perf_counter_now);
110 return perf_counter_now.QuadPart; 110 return perf_counter_now.QuadPart;
111 } 111 }
112 112
113 bool SafeConvertToWord(int in, WORD* out) {
114 base::CheckedNumeric<WORD> result = in;
115 *out = result.ValueOrDefault(std::numeric_limits<WORD>::max());
116 return result.IsValid();
117 }
118
113 } // namespace 119 } // namespace
114 120
115 // Time ----------------------------------------------------------------------- 121 // Time -----------------------------------------------------------------------
116 122
117 // The internal representation of Time uses FILETIME, whose epoch is 1601-01-01 123 // The internal representation of Time uses FILETIME, whose epoch is 1601-01-01
118 // 00:00:00 UTC. ((1970-1601)*365+89)*24*60*60*1000*1000, where 89 is the 124 // 00:00:00 UTC. ((1970-1601)*365+89)*24*60*60*1000*1000, where 89 is the
119 // number of leap year days between 1601 and 1970: (1970-1601)/4 excluding 125 // number of leap year days between 1601 and 1970: (1970-1601)/4 excluding
120 // 1700, 1800, and 1900. 126 // 1700, 1800, and 1900.
121 // static 127 // static
122 const int64_t Time::kTimeTToMicrosecondsOffset = INT64_C(11644473600000000); 128 const int64_t Time::kTimeTToMicrosecondsOffset = INT64_C(11644473600000000);
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 237
232 // static 238 // static
233 bool Time::IsHighResolutionTimerInUse() { 239 bool Time::IsHighResolutionTimerInUse() {
234 base::AutoLock lock(g_high_res_lock.Get()); 240 base::AutoLock lock(g_high_res_lock.Get());
235 return g_high_res_timer_enabled && g_high_res_timer_count > 0; 241 return g_high_res_timer_enabled && g_high_res_timer_count > 0;
236 } 242 }
237 243
238 // static 244 // static
239 bool Time::FromExploded(bool is_local, const Exploded& exploded, Time* time) { 245 bool Time::FromExploded(bool is_local, const Exploded& exploded, Time* time) {
240 // Create the system struct representing our exploded time. It will either be 246 // Create the system struct representing our exploded time. It will either be
241 // in local time or UTC. 247 // in local time or UTC.If casting from int to WORD results in overflow,
248 // fail and return Time(0).
242 SYSTEMTIME st; 249 SYSTEMTIME st;
243 st.wYear = static_cast<WORD>(exploded.year); 250 if (!SafeConvertToWord(exploded.year, &st.wYear) ||
244 st.wMonth = static_cast<WORD>(exploded.month); 251 !SafeConvertToWord(exploded.month, &st.wMonth) ||
245 st.wDayOfWeek = static_cast<WORD>(exploded.day_of_week); 252 !SafeConvertToWord(exploded.day_of_week, &st.wDayOfWeek) ||
246 st.wDay = static_cast<WORD>(exploded.day_of_month); 253 !SafeConvertToWord(exploded.day_of_month, &st.wDay) ||
247 st.wHour = static_cast<WORD>(exploded.hour); 254 !SafeConvertToWord(exploded.hour, &st.wHour) ||
248 st.wMinute = static_cast<WORD>(exploded.minute); 255 !SafeConvertToWord(exploded.minute, &st.wMinute) ||
249 st.wSecond = static_cast<WORD>(exploded.second); 256 !SafeConvertToWord(exploded.second, &st.wSecond) ||
250 st.wMilliseconds = static_cast<WORD>(exploded.millisecond); 257 !SafeConvertToWord(exploded.millisecond, &st.wMilliseconds)) {
258 *time = base::Time(0);
259 return false;
260 }
251 261
252 FILETIME ft; 262 FILETIME ft;
253 bool success = true; 263 bool success = true;
254 // Ensure that it's in UTC. 264 // Ensure that it's in UTC.
255 if (is_local) { 265 if (is_local) {
256 SYSTEMTIME utc_st; 266 SYSTEMTIME utc_st;
257 success = TzSpecificLocalTimeToSystemTime(nullptr, &st, &utc_st) && 267 success = TzSpecificLocalTimeToSystemTime(nullptr, &st, &utc_st) &&
258 SystemTimeToFileTime(&utc_st, &ft); 268 SystemTimeToFileTime(&utc_st, &ft);
259 } else { 269 } else {
260 success = !!SystemTimeToFileTime(&st, &ft); 270 success = !!SystemTimeToFileTime(&st, &ft);
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after
664 TimeTicks TimeTicks::FromQPCValue(LONGLONG qpc_value) { 674 TimeTicks TimeTicks::FromQPCValue(LONGLONG qpc_value) {
665 return TimeTicks() + QPCValueToTimeDelta(qpc_value); 675 return TimeTicks() + QPCValueToTimeDelta(qpc_value);
666 } 676 }
667 677
668 // TimeDelta ------------------------------------------------------------------ 678 // TimeDelta ------------------------------------------------------------------
669 679
670 // static 680 // static
671 TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) { 681 TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) {
672 return QPCValueToTimeDelta(qpc_value); 682 return QPCValueToTimeDelta(qpc_value);
673 } 683 }
OLDNEW
« no previous file with comments | « base/time/time_unittest.cc ('k') | components/data_reduction_proxy/core/browser/data_usage_store_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698