Fix TestSleep integer overflows identified by Coverity
Change-Id: Ibbc218100ea8a58c201bc6812cabc88dfd16f36e
This commit is contained in:
@@ -30,7 +30,7 @@ TestSleep::CallBack* TestSleep::callback_ = nullptr;
|
|||||||
int TestSleep::total_clock_rollback_seconds_ = 0;
|
int TestSleep::total_clock_rollback_seconds_ = 0;
|
||||||
|
|
||||||
void TestSleep::Sleep(unsigned int seconds) {
|
void TestSleep::Sleep(unsigned int seconds) {
|
||||||
int64_t milliseconds = 1000 * seconds;
|
int64_t milliseconds = 1000 * static_cast<int64_t>(seconds);
|
||||||
if (real_sleep_) {
|
if (real_sleep_) {
|
||||||
// This next bit of logic is to avoid slow drift apart of the real clock and
|
// This next bit of logic is to avoid slow drift apart of the real clock and
|
||||||
// the fake clock. We compute how far from the real clock has advanced in
|
// the fake clock. We compute how far from the real clock has advanced in
|
||||||
@@ -40,7 +40,8 @@ void TestSleep::Sleep(unsigned int seconds) {
|
|||||||
static const auto start_real = std::chrono::system_clock().now();
|
static const auto start_real = std::chrono::system_clock().now();
|
||||||
sleep(seconds);
|
sleep(seconds);
|
||||||
const auto now_real = std::chrono::system_clock().now();
|
const auto now_real = std::chrono::system_clock().now();
|
||||||
const int64_t rollback_adjustment = 1000 * total_clock_rollback_seconds_;
|
const int64_t rollback_adjustment =
|
||||||
|
1000 * static_cast<int64_t>(total_clock_rollback_seconds_);
|
||||||
const int64_t total_real =
|
const int64_t total_real =
|
||||||
(now_real - start_real) / std::chrono::milliseconds(1) +
|
(now_real - start_real) / std::chrono::milliseconds(1) +
|
||||||
rollback_adjustment;
|
rollback_adjustment;
|
||||||
@@ -129,7 +130,9 @@ bool TestSleep::RollbackSystemTime(int seconds) {
|
|||||||
// For both real and fake sleep we still update the callback and we still keep
|
// For both real and fake sleep we still update the callback and we still keep
|
||||||
// track of the total amount of time slept.
|
// track of the total amount of time slept.
|
||||||
total_clock_rollback_seconds_ += seconds;
|
total_clock_rollback_seconds_ += seconds;
|
||||||
if (callback_ != nullptr) callback_->ElapseTime(-1000 * seconds);
|
if (callback_ != nullptr) {
|
||||||
|
callback_->ElapseTime(-1000 * static_cast<int64_t>(seconds));
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user