SystemTimeNanos() on Mac: DCHECK that multiplication doesn't overflow
Bug: webrtc:6149
Change-Id: I1a58b60838d924e40e89126110cc19e16c953e80
Reviewed-on: https://webrtc-review.googlesource.com/56701
Commit-Queue: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Reviewed-by: Björn Terelius <terelius@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22194}
diff --git a/rtc_base/timeutils.cc b/rtc_base/timeutils.cc
index f924d5f..35c25c7 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 @@
}
}
// 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<int64_t>::max() / b)
+ << "The multiplication " << a << " * " << b << " overflows";
+ return rtc::dchecked_cast<int64_t>(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