diff --git a/rtc_base/timeutils.cc b/rtc_base/timeutils.cc index f924d5fddc..35c25c762c 100644 --- a/rtc_base/timeutils.cc +++ b/rtc_base/timeutils.cc @@ -27,6 +27,7 @@ #endif #include "rtc_base/checks.h" +#include "rtc_base/numerics/safe_conversions.h" #include "rtc_base/timeutils.h" namespace rtc { @@ -55,7 +56,13 @@ int64_t SystemTimeNanos() { } } // Use timebase to convert absolute time tick units into nanoseconds. - ticks = mach_absolute_time() * timebase.numer / timebase.denom; + const auto mul = [](uint64_t a, uint32_t b) -> int64_t { + RTC_DCHECK_NE(b, 0); + RTC_DCHECK_LE(a, std::numeric_limits::max() / b) + << "The multiplication " << a << " * " << b << " overflows"; + return rtc::dchecked_cast(a * b); + }; + ticks = mul(mach_absolute_time(), timebase.numer) / timebase.denom; #elif defined(WEBRTC_POSIX) struct timespec ts; // TODO(deadbeef): Do we need to handle the case when CLOCK_MONOTONIC is not