summaryrefslogtreecommitdiff
path: root/random.c
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2021-01-10 21:29:07 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2021-01-10 21:29:07 +0900
commitc63552eab1a13c6c05e18d763c7acf3c4c5383ed (patch)
tree1f63c76555b9f291fa4e3f965c1ec728bc993452 /random.c
parent1e827d4cb579e8c38daeaccf89338352fa5518eb (diff)
Remove possibility of using same seeds
Diffstat (limited to 'random.c')
-rw-r--r--random.c4
1 files changed, 2 insertions, 2 deletions
diff --git a/random.c b/random.c
index 29491fa..4b5965f 100644
--- a/random.c
+++ b/random.c
@@ -583,7 +583,7 @@ ruby_fill_random_bytes(void *seed, size_t size, int need_secure)
static void
fill_random_seed(uint32_t *seed, size_t cnt)
{
- static int n = 0;
+ static rb_atomic_t n = 0;
#if defined HAVE_CLOCK_GETTIME
struct timespec tv;
#elif defined HAVE_GETTIMEOFDAY
@@ -606,7 +606,7 @@ fill_random_seed(uint32_t *seed, size_t cnt)
#if SIZEOF_TIME_T > SIZEOF_INT
seed[0] ^= (uint32_t)((time_t)tv.tv_sec >> SIZEOF_INT * CHAR_BIT);
#endif
- seed[2] ^= getpid() ^ (n++ << 16);
+ seed[2] ^= getpid() ^ (ATOMIC_FETCH_ADD(n, 1) << 16);
seed[3] ^= (uint32_t)(VALUE)&seed;
#if SIZEOF_VOIDP > SIZEOF_INT
seed[2] ^= (uint32_t)((VALUE)&seed >> SIZEOF_INT * CHAR_BIT);