Skip to content

Commit c49c8ac

Browse files
committed
Merge branch 'dev' into dev2
2 parents 992ebd4 + 47bf3a5 commit c49c8ac

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/random.c

+13-9
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,17 @@ void _mi_random_split(mi_random_ctx_t* ctx, mi_random_ctx_t* ctx_new) {
143143

144144
uintptr_t _mi_random_next(mi_random_ctx_t* ctx) {
145145
mi_assert_internal(mi_random_is_initialized(ctx));
146-
#if MI_INTPTR_SIZE <= 4
147-
return chacha_next32(ctx);
148-
#elif MI_INTPTR_SIZE == 8
149-
return (((uintptr_t)chacha_next32(ctx) << 32) | chacha_next32(ctx));
150-
#else
151-
# error "define mi_random_next for this platform"
152-
#endif
146+
uintptr_t r;
147+
do {
148+
#if MI_INTPTR_SIZE <= 4
149+
r = chacha_next32(ctx);
150+
#elif MI_INTPTR_SIZE == 8
151+
r = (((uintptr_t)chacha_next32(ctx) << 32) | chacha_next32(ctx));
152+
#else
153+
# error "define mi_random_next for this platform"
154+
#endif
155+
} while (r==0);
156+
return r;
153157
}
154158

155159

@@ -163,7 +167,7 @@ uintptr_t _mi_os_random_weak(uintptr_t extra_seed) {
163167
x ^= _mi_prim_clock_now();
164168
// and do a few randomization steps
165169
uintptr_t max = ((x ^ (x >> 17)) & 0x0F) + 1;
166-
for (uintptr_t i = 0; i < max; i++) {
170+
for (uintptr_t i = 0; i < max || x==0; i++, x++) {
167171
x = _mi_random_shuffle(x);
168172
}
169173
mi_assert_internal(x != 0);
@@ -179,7 +183,7 @@ static void mi_random_init_ex(mi_random_ctx_t* ctx, bool use_weak) {
179183
if (!use_weak) { _mi_warning_message("unable to use secure randomness\n"); }
180184
#endif
181185
uintptr_t x = _mi_os_random_weak(0);
182-
for (size_t i = 0; i < 8; i++) { // key is eight 32-bit words.
186+
for (size_t i = 0; i < 8; i++, x++) { // key is eight 32-bit words.
183187
x = _mi_random_shuffle(x);
184188
((uint32_t*)key)[i] = (uint32_t)x;
185189
}

0 commit comments

Comments
 (0)