Revision: 21828
Author:   [email protected]
Date:     Fri Jun 13 06:36:09 2014 UTC
Log:      Fixed undefined behavior in RNG.

We're basically trading undefined behavior for implementation defined
behavior, which should be OK for UBSan. :-) The generated code should
be identical, at least I checked that for GCC 4.6.3 on x64.

BUG=377790
LOG=y
[email protected]

Review URL: https://codereview.chromium.org/332733002
http://code.google.com/p/v8/source/detail?r=21828

Modified:
 /branches/bleeding_edge/src/utils/random-number-generator.cc

=======================================
--- /branches/bleeding_edge/src/utils/random-number-generator.cc Tue Jun 3 08:12:43 2014 UTC +++ /branches/bleeding_edge/src/utils/random-number-generator.cc Fri Jun 13 06:36:09 2014 UTC
@@ -117,7 +117,13 @@
 int RandomNumberGenerator::Next(int bits) {
   ASSERT_LT(0, bits);
   ASSERT_GE(32, bits);
-  int64_t seed = (seed_ * kMultiplier + kAddend) & kMask;
+ // Do unsigned multiplication, which has the intended modulo semantics, while
+  // signed multiplication would expose undefined behavior.
+  uint64_t product = static_cast<uint64_t>(seed_) * kMultiplier;
+  // Assigning a uint64_t to an int64_t is implementation defined, but this
+ // should be OK. Use a static_cast to explicitly state that we know what we're
+  // doing. (Famous last words...)
+  int64_t seed = static_cast<int64_t>((product + kAddend) & kMask);
   seed_ = seed;
   return static_cast<int>(seed >> (48 - bits));
 }

--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to