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

Unified Diff: webrtc/base/random_unittest.cc

Issue 1744183002: Fix some signed overflow errors causing undefined behavior (in theory). (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Format and comment Created 4 years, 10 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/base/mathutils.h ('k') | webrtc/base/rollingaccumulator.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/base/random_unittest.cc
diff --git a/webrtc/base/random_unittest.cc b/webrtc/base/random_unittest.cc
index 3b47a00806eb0fc29ae0e43568df0f8b19fde960..ae1a0a2f5122004399404afa7ea316f7c8a52c8c 100644
--- a/webrtc/base/random_unittest.cc
+++ b/webrtc/base/random_unittest.cc
@@ -14,6 +14,7 @@
#include <vector>
#include "testing/gtest/include/gtest/gtest.h"
+#include "webrtc/base/mathutils.h" // unsigned difference
#include "webrtc/base/random.h"
namespace webrtc {
@@ -118,7 +119,7 @@ void BucketTestSignedInterval(unsigned int bucket_count,
ASSERT_GE(high, low);
ASSERT_GE(bucket_count, 2u);
- uint32_t interval = static_cast<uint32_t>(high - low + 1);
+ uint32_t interval = unsigned_difference<int32_t>(high, low) + 1;
uint32_t numbers_per_bucket;
if (interval == 0) {
// The computation high - low + 1 should be 2^32 but overflowed
@@ -134,7 +135,7 @@ void BucketTestSignedInterval(unsigned int bucket_count,
int32_t sample = prng->Rand(low, high);
EXPECT_LE(low, sample);
EXPECT_GE(high, sample);
- buckets[static_cast<uint32_t>(sample - low) / numbers_per_bucket]++;
+ buckets[unsigned_difference<int32_t>(sample, low) / numbers_per_bucket]++;
}
for (unsigned int i = 0; i < bucket_count; i++) {
@@ -158,7 +159,7 @@ void BucketTestUnsignedInterval(unsigned int bucket_count,
ASSERT_GE(high, low);
ASSERT_GE(bucket_count, 2u);
- uint32_t interval = static_cast<uint32_t>(high - low + 1);
+ uint32_t interval = high - low + 1;
uint32_t numbers_per_bucket;
if (interval == 0) {
// The computation high - low + 1 should be 2^32 but overflowed
@@ -174,7 +175,7 @@ void BucketTestUnsignedInterval(unsigned int bucket_count,
uint32_t sample = prng->Rand(low, high);
EXPECT_LE(low, sample);
EXPECT_GE(high, sample);
- buckets[static_cast<uint32_t>(sample - low) / numbers_per_bucket]++;
+ buckets[(sample - low) / numbers_per_bucket]++;
}
for (unsigned int i = 0; i < bucket_count; i++) {
« no previous file with comments | « webrtc/base/mathutils.h ('k') | webrtc/base/rollingaccumulator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698