@@ -143,13 +143,17 @@ void _mi_random_split(mi_random_ctx_t* ctx, mi_random_ctx_t* ctx_new) {
143
143
144
144
uintptr_t _mi_random_next (mi_random_ctx_t * ctx ) {
145
145
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 ;
153
157
}
154
158
155
159
@@ -163,7 +167,7 @@ uintptr_t _mi_os_random_weak(uintptr_t extra_seed) {
163
167
x ^= _mi_prim_clock_now ();
164
168
// and do a few randomization steps
165
169
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 ++ ) {
167
171
x = _mi_random_shuffle (x );
168
172
}
169
173
mi_assert_internal (x != 0 );
@@ -179,7 +183,7 @@ static void mi_random_init_ex(mi_random_ctx_t* ctx, bool use_weak) {
179
183
if (!use_weak ) { _mi_warning_message ("unable to use secure randomness\n" ); }
180
184
#endif
181
185
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.
183
187
x = _mi_random_shuffle (x );
184
188
((uint32_t * )key )[i ] = (uint32_t )x ;
185
189
}
0 commit comments