Skip to content

Commit 12c9199

Browse files
committed
Merge branch 'dev' into dev-slice
2 parents 774d12f + adc8b31 commit 12c9199

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/random.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,10 @@ If we cannot get good randomness, we fall back to weak randomness based on a tim
168168

169169
#if defined(_WIN32)
170170

171-
#if !defined(MI_USE_RTLGENRANDOM)
172-
// We prefer to use BCryptGenRandom instead of RtlGenRandom but it can lead to a deadlock
173-
// under the VS debugger when using dynamic overriding.
174-
#pragma comment (lib,"bcrypt.lib")
175-
#include <bcrypt.h>
176-
static bool os_random_buf(void* buf, size_t buf_len) {
177-
return (BCryptGenRandom(NULL, (PUCHAR)buf, (ULONG)buf_len, BCRYPT_USE_SYSTEM_PREFERRED_RNG) >= 0);
178-
}
179-
#else
180-
// Use (unofficial) RtlGenRandom
171+
#if defined(MI_USE_RTLGENRANDOM) || defined(__cplusplus)
172+
// We prefer to use BCryptGenRandom instead of (the unofficial) RtlGenRandom but when using
173+
// dynamic overriding, we observed it can raise an exception when compiled with C++, and
174+
// sometimes deadlocks when also running under the VS debugger.
181175
#pragma comment (lib,"advapi32.lib")
182176
#define RtlGenRandom SystemFunction036
183177
#ifdef __cplusplus
@@ -190,6 +184,12 @@ BOOLEAN NTAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength);
190184
static bool os_random_buf(void* buf, size_t buf_len) {
191185
return (RtlGenRandom(buf, (ULONG)buf_len) != 0);
192186
}
187+
#else
188+
#pragma comment (lib,"bcrypt.lib")
189+
#include <bcrypt.h>
190+
static bool os_random_buf(void* buf, size_t buf_len) {
191+
return (BCryptGenRandom(NULL, (PUCHAR)buf, (ULONG)buf_len, BCRYPT_USE_SYSTEM_PREFERRED_RNG) >= 0);
192+
}
193193
#endif
194194

195195
#elif defined(__APPLE__)

0 commit comments

Comments
 (0)