Fix TestSleep integer overflows identified by Coverity

Change-Id: Ibbc218100ea8a58c201bc6812cabc88dfd16f36e
This commit is contained in:
Ian Benz
2024-01-19 23:56:56 +00:00
committed by Robert Shih
parent c232299f78
commit a0f6b99cbe

View File

@@ -30,7 +30,7 @@ TestSleep::CallBack* TestSleep::callback_ = nullptr;
int TestSleep::total_clock_rollback_seconds_ = 0;
void TestSleep::Sleep(unsigned int seconds) {
int64_t milliseconds = 1000 * seconds;
int64_t milliseconds = 1000 * static_cast<int64_t>(seconds);
if (real_sleep_) {
// 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
@@ -40,7 +40,8 @@ void TestSleep::Sleep(unsigned int seconds) {
static const auto start_real = std::chrono::system_clock().now();
sleep(seconds);
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 =
(now_real - start_real) / std::chrono::milliseconds(1) +
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
// track of the total amount of time slept.
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;
}