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

Unified Diff: base/rand_util.cc

Issue 1419703005: Add missing overflow handling to base::RandInt(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 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 | « no previous file | base/rand_util_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/rand_util.cc
diff --git a/base/rand_util.cc b/base/rand_util.cc
index 931eb4e943e646e2f39e2848b33683247b5b4725..655ff5bbde6fb51aba66472bab55e78bf04dd20a 100644
--- a/base/rand_util.cc
+++ b/base/rand_util.cc
@@ -20,7 +20,10 @@ int RandInt(int min, int max) {
DCHECK_LE(min, max);
uint64 range = static_cast<uint64>(max) - min + 1;
- int result = min + static_cast<int>(base::RandGenerator(range));
+ // |range| is at most UINT_MAX + 1, so the result of RandGenerator(range)
+ // is at most UINT_MAX. Hence it's safe to cast it from uint64 to int64.
+ int result =
+ static_cast<int>(min + static_cast<int64>(base::RandGenerator(range)));
Lei Zhang 2015/10/29 16:38:15 int64_t, and fix line 22 as well?
Nico 2015/10/29 16:46:45 Done.
DCHECK_GE(result, min);
DCHECK_LE(result, max);
return result;
« no previous file with comments | « no previous file | base/rand_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698