Bug 1119403 - Suppress read() -Wunused-result warning in jsmath.cpp's PRNG and call rand_s() twice. r=luke
authorChris Peterson <cpeterson@mozilla.com>
Thu, 08 Jan 2015 15:01:18 -0800
changeset 223730 4c63c749f2ca48fe59d503efd96a2e8277133c1f
parent 223729 48023b20197dea388202ef41bf009ab5897ebdec
child 223731 ce75473a8e87fafb3243071629f864c0e0e90e3c
push id10811
push usercbook@mozilla.com
push dateWed, 14 Jan 2015 13:52:17 +0000
treeherderfx-team@b8d38d19dbd9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersluke
bugs1119403
milestone38.0a1
Bug 1119403 - Suppress read() -Wunused-result warning in jsmath.cpp's PRNG and call rand_s() twice. r=luke
js/src/jsmath.cpp
--- a/js/src/jsmath.cpp
+++ b/js/src/jsmath.cpp
@@ -9,16 +9,17 @@
  */
 
 #include "jsmath.h"
 
 #include "mozilla/Constants.h"
 #include "mozilla/FloatingPoint.h"
 #include "mozilla/MathAlgorithms.h"
 #include "mozilla/MemoryReporting.h"
+#include "mozilla/unused.h"
 
 #include <algorithm>  // for std::max
 #include <fcntl.h>
 
 #ifdef XP_UNIX
 # include <unistd.h>
 #endif
 
@@ -719,38 +720,40 @@ random_generateSeed()
     union {
         uint8_t     u8[8];
         uint32_t    u32[2];
         uint64_t    u64;
     } seed;
     seed.u64 = 0;
 
 #if defined(XP_WIN)
-    /*
-     * Our PRNG only uses 48 bits, so calling rand_s() twice to get 64 bits is
-     * probably overkill.
-     */
-    rand_s(&seed.u32[0]);
+    errno_t error = rand_s(&seed.u32[0]);
+    MOZ_ASSERT(error == 0, "rand_s() error?!");
+
+    error = rand_s(&seed.u32[1]);
+    MOZ_ASSERT(error == 0, "rand_s() error?!");
 #elif defined(XP_UNIX)
     /*
      * In the unlikely event we can't read /dev/urandom, there's not much we can
      * do, so just mix in the fd error code and the current time.
      */
     int fd = open("/dev/urandom", O_RDONLY);
-    MOZ_ASSERT(fd >= 0, "Can't open /dev/urandom");
+    MOZ_ASSERT(fd >= 0, "Can't open /dev/urandom?!");
     if (fd >= 0) {
-        (void)read(fd, seed.u8, mozilla::ArrayLength(seed.u8));
+        ssize_t nread = read(fd, seed.u8, mozilla::ArrayLength(seed.u8));
+        MOZ_ASSERT(nread == 8, "Can't read /dev/urandom?!");
+        mozilla::unused << nread;
         close(fd);
     }
     seed.u32[0] ^= fd;
 #else
 # error "Platform needs to implement random_generateSeed()"
 #endif
 
-    seed.u32[1] ^= PRMJ_Now();
+    seed.u64 ^= PRMJ_Now();
     return seed.u64;
 }
 
 static const uint64_t RNG_MULTIPLIER = 0x5DEECE66DLL;
 static const uint64_t RNG_ADDEND = 0xBLL;
 static const uint64_t RNG_MASK = (1LL << 48) - 1;
 
 /*
@@ -974,17 +977,16 @@ js::math_tan(JSContext *cx, unsigned arg
     if (!mathCache)
         return false;
 
     double z = math_tan_impl(mathCache, x);
     args.rval().setDouble(z);
     return true;
 }
 
-
 typedef double (*UnaryMathFunctionType)(MathCache *cache, double);
 
 template <UnaryMathFunctionType F>
 static bool math_function(JSContext *cx, unsigned argc, Value *vp)
 {
     CallArgs args = CallArgsFromVp(argc, vp);
     if (args.length() == 0) {
         args.rval().setNumber(GenericNaN());
@@ -999,18 +1001,16 @@ static bool math_function(JSContext *cx,
     if (!mathCache)
         return false;
     double z = F(mathCache, x);
     args.rval().setNumber(z);
 
     return true;
 }
 
-
-
 double
 js::math_log10_impl(MathCache *cache, double x)
 {
     return cache->lookup(log10, x, MathCache::Log10);
 }
 
 double
 js::math_log10_uncached(double x)
@@ -1140,17 +1140,16 @@ double sqrt1pm1(double x)
 {
     if (fabs(x) > 0.75)
         return sqrt(1 + x) - 1;
 
     return expm1(log1p(x) / 2);
 }
 #endif
 
-
 double
 js::math_cosh_impl(MathCache *cache, double x)
 {
     return cache->lookup(cosh, x, MathCache::Cosh);
 }
 
 double
 js::math_cosh_uncached(double x)