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

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

Issue 27472003: android: fix base::Time::FromLocalExploded() crash. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use new code paths on all platforms. Created 7 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 unified diff | Download patch
« no previous file with comments | « no previous file | base/time/time_unittest.cc » ('j') | base/time/time_unittest.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "base/time/time.h" 5 #include "base/time/time.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <sys/time.h> 8 #include <sys/time.h>
9 #include <time.h> 9 #include <time.h>
10 #if defined(OS_ANDROID) 10 #if defined(OS_ANDROID)
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 timestruct.tm_mon = exploded.month - 1; 207 timestruct.tm_mon = exploded.month - 1;
208 timestruct.tm_year = exploded.year - 1900; 208 timestruct.tm_year = exploded.year - 1900;
209 timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this 209 timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this
210 timestruct.tm_yday = 0; // mktime/timegm ignore this 210 timestruct.tm_yday = 0; // mktime/timegm ignore this
211 timestruct.tm_isdst = -1; // attempt to figure it out 211 timestruct.tm_isdst = -1; // attempt to figure it out
212 #if !defined(OS_NACL) && !defined(OS_SOLARIS) 212 #if !defined(OS_NACL) && !defined(OS_SOLARIS)
213 timestruct.tm_gmtoff = 0; // not a POSIX field, so mktime/timegm ignore 213 timestruct.tm_gmtoff = 0; // not a POSIX field, so mktime/timegm ignore
214 timestruct.tm_zone = NULL; // not a POSIX field, so mktime/timegm ignore 214 timestruct.tm_zone = NULL; // not a POSIX field, so mktime/timegm ignore
215 #endif 215 #endif
216 216
217 SysTime seconds = SysTimeFromTimeStruct(&timestruct, is_local);
218 217
219 int64 milliseconds; 218 int64 milliseconds;
219 SysTime seconds;
220
221 // Certain exploded dates do not really exist due to daylight saving times,
222 // and this causes mktime() to return implementation-defined values when
223 // tm_isdst is set to -1. On Android, the function will return -1, while the
224 // C libraries of other platforms typically return a liberally-chosen value.
225 // Handling this requires the special code below.
226
227 // SysTimeFromTimeStruct() modifies the input structure, save current value.
228 struct tm timestruct0 = timestruct;
229
230 seconds = SysTimeFromTimeStruct(&timestruct, is_local);
231 if (seconds == -1) {
232 // Get the time values with tm_isdst == 0 and 1, then select the closest one
233 // to UTC 00:00:00 that isn't -1.
234 timestruct = timestruct0;
235 timestruct.tm_isdst = 0;
236 int64 seconds_isdst0 = SysTimeFromTimeStruct(&timestruct, is_local);
237
238 timestruct = timestruct0;
239 timestruct.tm_isdst = 1;
240 int64 seconds_isdst1 = SysTimeFromTimeStruct(&timestruct, is_local);
241
242 // seconds_isdst0 or seconds_isdst1 can be -1 for some timezones.
243 // E.g. "CLST" (Chile Summer Time) returns -1 for 'tm_isdt == 1'.
244 if (seconds_isdst0 < 0)
245 seconds = seconds_isdst1;
246 else if (seconds_isdst1 < 0)
247 seconds = seconds_isdst0;
248 else
249 seconds = std::min(seconds_isdst0, seconds_isdst1);
250 }
251
220 // Handle overflow. Clamping the range to what mktime and timegm might 252 // Handle overflow. Clamping the range to what mktime and timegm might
221 // return is the best that can be done here. It's not ideal, but it's better 253 // return is the best that can be done here. It's not ideal, but it's better
222 // than failing here or ignoring the overflow case and treating each time 254 // than failing here or ignoring the overflow case and treating each time
223 // overflow as one second prior to the epoch. 255 // overflow as one second prior to the epoch.
224 if (seconds == -1 && 256 if (seconds == -1 &&
225 (exploded.year < 1969 || exploded.year > 1970)) { 257 (exploded.year < 1969 || exploded.year > 1970)) {
226 // If exploded.year is 1969 or 1970, take -1 as correct, with the 258 // If exploded.year is 1969 or 1970, take -1 as correct, with the
227 // time indicating 1 second prior to the epoch. (1970 is allowed to handle 259 // time indicating 1 second prior to the epoch. (1970 is allowed to handle
228 // time zone and DST offsets.) Otherwise, return the most future or past 260 // time zone and DST offsets.) Otherwise, return the most future or past
229 // time representable. Assumes the time_t epoch is 1970-01-01 00:00:00 UTC. 261 // time representable. Assumes the time_t epoch is 1970-01-01 00:00:00 UTC.
230 // 262 //
231 // The minimum and maximum representible times that mktime and timegm could 263 // The minimum and maximum representible times that mktime and timegm could
232 // return are used here instead of values outside that range to allow for 264 // return are used here instead of values outside that range to allow for
233 // proper round-tripping between exploded and counter-type time 265 // proper round-tripping between exploded and counter-type time
234 // representations in the presence of possible truncation to time_t by 266 // representations in the presence of possible truncation to time_t by
235 // division and use with other functions that accept time_t. 267 // division and use with other functions that accept time_t.
236 // 268 //
237 // When representing the most distant time in the future, add in an extra 269 // When representing the most distant time in the future, add in an extra
238 // 999ms to avoid the time being less than any other possible value that 270 // 999ms to avoid the time being less than any other possible value that
239 // this function can return. 271 // this function can return.
272
273 // On Android, SysTime is int64, special care must be taken to avoid
274 // overflows.
275 const int64 min_seconds = (sizeof(SysTime) < sizeof(int64))
276 ? std::numeric_limits<SysTime>::min()
277 : std::numeric_limits<int32_t>::min();
278 const int64 max_seconds = (sizeof(SysTime) < sizeof(int64))
279 ? std::numeric_limits<SysTime>::max()
280 : std::numeric_limits<int32_t>::max();
240 if (exploded.year < 1969) { 281 if (exploded.year < 1969) {
241 CHECK(sizeof(SysTime) < sizeof(int64)) << "integer overflow"; 282 milliseconds = min_seconds * kMillisecondsPerSecond;
242 milliseconds = std::numeric_limits<SysTime>::min();
243 milliseconds *= kMillisecondsPerSecond;
244 } else { 283 } else {
245 CHECK(sizeof(SysTime) < sizeof(int64)) << "integer overflow"; 284 milliseconds = max_seconds * kMillisecondsPerSecond;
246 milliseconds = std::numeric_limits<SysTime>::max();
247 milliseconds *= kMillisecondsPerSecond;
248 milliseconds += (kMillisecondsPerSecond - 1); 285 milliseconds += (kMillisecondsPerSecond - 1);
249 } 286 }
250 } else { 287 } else {
251 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond; 288 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond;
252 } 289 }
253 290
254 // Adjust from Unix (1970) to Windows (1601) epoch. 291 // Adjust from Unix (1970) to Windows (1601) epoch.
255 return Time((milliseconds * kMicrosecondsPerMillisecond) + 292 return Time((milliseconds * kMicrosecondsPerMillisecond) +
256 kWindowsEpochDeltaMicroseconds); 293 kWindowsEpochDeltaMicroseconds);
257 } 294 }
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; 380 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1;
344 return result; 381 return result;
345 } 382 }
346 int64 us = us_ - kTimeTToMicrosecondsOffset; 383 int64 us = us_ - kTimeTToMicrosecondsOffset;
347 result.tv_sec = us / Time::kMicrosecondsPerSecond; 384 result.tv_sec = us / Time::kMicrosecondsPerSecond;
348 result.tv_usec = us % Time::kMicrosecondsPerSecond; 385 result.tv_usec = us % Time::kMicrosecondsPerSecond;
349 return result; 386 return result;
350 } 387 }
351 388
352 } // namespace base 389 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/time/time_unittest.cc » ('j') | base/time/time_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698