From e0269cd0a646953a5bd3b10f2760b43c3478cc69 Mon Sep 17 00:00:00 2001 From: Karl Wiberg Date: Mon, 26 Feb 2018 23:44:19 +0100 Subject: [PATCH] SystemTimeNanos() on Mac: DCHECK that multiplication doesn't overflow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: webrtc:6149 Change-Id: I1a58b60838d924e40e89126110cc19e16c953e80 Reviewed-on: https://webrtc-review.googlesource.com/56701 Commit-Queue: Karl Wiberg Reviewed-by: Niels Moller Reviewed-by: Björn Terelius Cr-Commit-Position: refs/heads/master@{#22194} --- rtc_base/timeutils.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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