Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3084)

Unified Diff: webrtc/rtc_base/random.cc

Issue 3009123002: Move UBSan warnings from a blacklist to the source (Closed)
Patch Set: Address review comments Created 3 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webrtc/rtc_base/ipaddress_unittest.cc ('k') | webrtc/rtc_base/random_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/rtc_base/random.cc
diff --git a/webrtc/rtc_base/random.cc b/webrtc/rtc_base/random.cc
index c0cf7fea71ac982ac5d653500935d44a9d31cb5b..f6bfc08a3e05b033973511022f7533ba79265b70 100644
--- a/webrtc/rtc_base/random.cc
+++ b/webrtc/rtc_base/random.cc
@@ -12,6 +12,7 @@
#include <math.h>
#include "webrtc/rtc_base/checks.h"
+#include "webrtc/rtc_base/safe_conversions.h"
namespace webrtc {
@@ -40,11 +41,9 @@ uint32_t Random::Rand(uint32_t low, uint32_t high) {
int32_t Random::Rand(int32_t low, int32_t high) {
RTC_DCHECK(low <= high);
- // We rely on subtraction (and addition) to be the same for signed and
- // unsigned numbers in two-complement representation. Thus, although
- // high - low might be negative as an int, it is the correct difference
- // when interpreted as an unsigned.
- return Rand(high - low) + low;
+ const int64_t low_i64{low};
+ return rtc::dchecked_cast<int32_t>(
+ Rand(rtc::dchecked_cast<uint32_t>(high - low_i64)) + low_i64);
}
template <>
« no previous file with comments | « webrtc/rtc_base/ipaddress_unittest.cc ('k') | webrtc/rtc_base/random_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698