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 <orphis@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Yves Gerey <yvesg@google.com>
Cr-Commit-Position: refs/heads/master@{#30132}
This commit is contained in:
Yves Gerey 2019-12-31 16:16:51 +01:00 committed by Commit Bot
parent cf4c872dbd
commit a688d11d96

View File

@ -11,6 +11,7 @@
#include "rtc_base/rate_statistics.h"
#include <algorithm>
#include <limits>
#include <memory>
#include "rtc_base/checks.h"
@ -91,7 +92,13 @@ absl::optional<uint32_t> RateStatistics::Rate(int64_t now_ms) const {
}
float scale = scale_ / active_window_size;
return static_cast<uint32_t>(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<uint32_t>::max()) {
return absl::nullopt;
}
return static_cast<uint32_t>(result);
}
void RateStatistics::EraseOld(int64_t now_ms) {