util/rand_xor: fallback Linux to time-based instead of fixed seed
authorEric Engestrom <eric.engestrom@intel.com>
Sun, 13 Oct 2019 15:09:22 +0000 (16:09 +0100)
committerMarge Bot <eric+marge@anholt.net>
Sat, 16 May 2020 12:05:37 +0000 (12:05 +0000)
When the caller asked for a randomised_seed, we should fall back to the
time-based seed instead of the fixed seed.

Signed-off-by: Eric Engestrom <eric.engestrom@intel.com>
Reviewed-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/2316>

src/util/rand_xor.c

index b486a53343cc40c0ef8bc48969856e8b3063e731..3668ac9b84a0309aec837642b71873cb3be687d1 100644 (file)
 #endif
 #include <unistd.h>
 #include <fcntl.h>
-#else
-#include <time.h>
 #endif
 
+#include <time.h>
+
 #include "rand_xor.h"
 
 /* Super fast random number generator.
@@ -56,8 +56,12 @@ rand_xorshift128plus(uint64_t seed[2])
 void
 s_rand_xorshift128plus(uint64_t seed[2], bool randomised_seed)
 {
-   if (!randomised_seed)
-      goto fixed_seed;
+   if (!randomised_seed) {
+      /* Use a fixed seed */
+      seed[0] = 0x3bffb83978e24f88;
+      seed[1] = 0x9238d5d56c71cd35;
+      return;
+   }
 
 #if defined(__linux__)
    size_t seed_size = sizeof(uint64_t) * 2;
@@ -69,27 +73,15 @@ s_rand_xorshift128plus(uint64_t seed[2], bool randomised_seed)
 #endif
 
    int fd = open("/dev/urandom", O_RDONLY);
-   if (fd < 0)
-      goto fixed_seed;
-
-   if (read(fd, seed, seed_size) != seed_size) {
+   if (fd >= 0) {
+      if (read(fd, seed, seed_size) == seed_size) {
+         close(fd);
+         return;
+      }
       close(fd);
-      goto fixed_seed;
    }
-
-   close(fd);
-   return;
-
-#else
-   seed[0] = 0x3bffb83978e24f88;
-   seed[1] = time(NULL);
-
-   return;
 #endif
 
-fixed_seed:
-
-   /* Fallback to a fixed seed */
    seed[0] = 0x3bffb83978e24f88;
-   seed[1] = 0x9238d5d56c71cd35;
+   seed[1] = time(NULL);
 }