Bug 1205942 (part 1) - Fix overflows in time_posix.cc. r=jld.

We get the following warnings with clang.

> ipc/chromium/src/base/time_posix.cc:103:57: error: overflow in expression; result is 0 with type 'long' [-Werror,-Winteger-overflow]
> ipc/chromium/src/base/time_posix.cc:106:58: error: overflow in expression; result is -1000 with type 'long' [-Werror,-Winteger-overflow]

This is a genuine bug. The upstream code in Chromium has changed (commit
2a278516943eee02e0206506a4b907fc0b55f27b) and this patch changes our code to be
similar. I did tests and confirmed that instead of getting 0 or -1 for
|milliseconds|, we now get -2147483648000 or 2147483647999, which is much
better.

--HG--
extra : rebase_source : f01a4f03bc1576980010426328116d03eb71079b
This commit is contained in:
Nicholas Nethercote 2015-09-21 15:35:26 -07:00
parent ce6cdc5ce3
commit 51aed272e1

View File

@ -99,13 +99,19 @@ Time Time::FromExploded(bool is_local, const Exploded& exploded) {
// When representing the most distant time in the future, add in an extra
// 999ms to avoid the time being less than any other possible value that
// this function can return.
// Take care to avoid overflows when time_t is int64_t.
if (exploded.year < 1969) {
milliseconds = std::numeric_limits<time_t>::min() *
kMillisecondsPerSecond;
int64_t min_seconds = (sizeof(time_t) < sizeof(int64_t))
? std::numeric_limits<time_t>::min()
: std::numeric_limits<int32_t>::min();
milliseconds = min_seconds * kMillisecondsPerSecond;
} else {
milliseconds = (std::numeric_limits<time_t>::max() *
kMillisecondsPerSecond) +
kMillisecondsPerSecond - 1;
int64_t max_seconds = (sizeof(time_t) < sizeof(int64_t))
? std::numeric_limits<time_t>::max()
: std::numeric_limits<int32_t>::max();
milliseconds = max_seconds * kMillisecondsPerSecond;
milliseconds += kMillisecondsPerSecond - 1;
}
} else {
milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond;