From a688d11d9696cf63c826b03bbc285bb9d8d72ae1 Mon Sep 17 00:00:00 2001 From: Yves Gerey Date: Tue, 31 Dec 2019 16:16:51 +0100 Subject: [PATCH] Return unavailable rate rather than garbage value. This CL quiets UBSan when value doesn't fit uint32_t. Bug: webrtc:11182 Change-Id: I8a45867be9aaceeb490db1a3747eb0efc6eb6a8f Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/163983 Reviewed-by: Florent Castelli Reviewed-by: Harald Alvestrand Commit-Queue: Yves Gerey Cr-Commit-Position: refs/heads/master@{#30132} --- rtc_base/rate_statistics.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/rtc_base/rate_statistics.cc b/rtc_base/rate_statistics.cc index b393dc863a..89f7e54a68 100644 --- a/rtc_base/rate_statistics.cc +++ b/rtc_base/rate_statistics.cc @@ -11,6 +11,7 @@ #include "rtc_base/rate_statistics.h" #include +#include #include #include "rtc_base/checks.h" @@ -91,7 +92,13 @@ absl::optional RateStatistics::Rate(int64_t now_ms) const { } float scale = scale_ / active_window_size; - return static_cast(accumulated_count_ * scale + 0.5f); + float result = accumulated_count_ * scale + 0.5f; + + // Better return unavailable rate than garbage value (undefined behavior). + if (result > std::numeric_limits::max()) { + return absl::nullopt; + } + return static_cast(result); } void RateStatistics::EraseOld(int64_t now_ms) {