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