Commit 877f46a8 authored by Qijiang Fan's avatar Qijiang Fan Committed by Commit Bot

Fix integer overflow if microseconds is too small

microseconds - kMicrosecondsPerMillisecond could cause an overflow if
microseconds is too small (close to INT64_MIN)

Bug: 1065504
Change-Id: I827dc3479eabbf1be5fdf964dbcb34bc48d7f401
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2129788Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Commit-Queue: Qijiang Fan <fqj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#754979}
parent ff8be2a8
......@@ -402,8 +402,7 @@ int64_t Time::ToRoundedDownMillisecondsSinceUnixEpoch() const {
// In this case, rounding towards -infinity means rounding towards 0.
return microseconds / kMicrosecondsPerMillisecond;
} else {
return (microseconds - kMicrosecondsPerMillisecond + 1) /
kMicrosecondsPerMillisecond;
return (microseconds + 1) / kMicrosecondsPerMillisecond - 1;
}
}
......
......@@ -106,9 +106,8 @@ void Time::Explode(bool is_local, Exploded* exploded) const {
// Calculate milliseconds ourselves, since we rounded the |seconds|, making
// sure to round towards -infinity.
exploded->millisecond =
(microsecond >= 0) ? microsecond / kMicrosecondsPerMillisecond :
(microsecond - kMicrosecondsPerMillisecond + 1) /
kMicrosecondsPerMillisecond;
(microsecond >= 0) ? microsecond / kMicrosecondsPerMillisecond
: ((microsecond + 1) / kMicrosecondsPerMillisecond - 1)
}
} // namespace base
......@@ -134,8 +134,7 @@ void Time::Explode(bool is_local, Exploded* exploded) const {
millisecond = milliseconds % kMillisecondsPerSecond;
} else {
// Round these *down* (towards -infinity).
seconds =
(milliseconds - kMillisecondsPerSecond + 1) / kMillisecondsPerSecond;
seconds = (milliseconds + 1) / kMillisecondsPerSecond - 1;
// Make this nonnegative (and between 0 and 999 inclusive).
millisecond = milliseconds % kMillisecondsPerSecond;
if (millisecond < 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment