From d76abe98cf15226f25d93e76e383715061ada6f4 Mon Sep 17 00:00:00 2001 From: Eric Engestrom Date: Sun, 13 Oct 2019 16:09:22 +0100 Subject: [PATCH] util/rand_xor: fallback Linux to time-based instead of fixed seed 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 Reviewed-by: Emmanuel Gil Peyrot Part-of: --- src/util/rand_xor.c | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/util/rand_xor.c b/src/util/rand_xor.c index b486a53343c..3668ac9b84a 100644 --- a/src/util/rand_xor.c +++ b/src/util/rand_xor.c @@ -28,10 +28,10 @@ #endif #include #include -#else -#include #endif +#include + #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); } -- 2.30.2