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}
This commit is contained in:
Karl Wiberg 2018-02-26 23:44:19 +01:00 committed by Commit Bot
parent d8243fa6b3
commit e0269cd0a6

View File

@ -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<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