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}
diff --git a/rtc_base/rate_statistics.cc b/rtc_base/rate_statistics.cc
index b393dc8..89f7e54 100644
--- a/rtc_base/rate_statistics.cc
+++ b/rtc_base/rate_statistics.cc
@@ -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 @@
   }
 
   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) {