|
19 | 19 |
|
20 | 20 | #if defined(_WIN32) && !defined(__CYGWIN__)
|
21 | 21 | #define WIN32_LEAN_AND_MEAN
|
| 22 | +#define NOMINMAX |
22 | 23 | #include <Windows.h>
|
23 | 24 | #include <Bcrypt.h>
|
24 | 25 | #pragma comment(lib, "bcrypt.lib")
|
@@ -65,16 +66,18 @@ void swift_stdlib_random(void *buf, __swift_size_t nbytes) {
|
65 | 66 |
|
66 | 67 | SWIFT_RUNTIME_STDLIB_API
|
67 | 68 | void swift_stdlib_random(void *buf, __swift_size_t nbytes) {
|
68 |
| - if (nbytes > ULONG_MAX) { |
69 |
| - fatalError(0, "Fatal error: %zd exceeds ULONG_MAX\n", nbytes); |
70 |
| - } |
| 69 | + while (nbytes > 0) { |
| 70 | + auto actual_nbytes = std::min(nbytes, (__swift_size_t)ULONG_MAX); |
| 71 | + NTSTATUS status = BCryptGenRandom(nullptr, |
| 72 | + static_cast<PUCHAR>(buf), |
| 73 | + static_cast<ULONG>(actual_nbytes), |
| 74 | + BCRYPT_USE_SYSTEM_PREFERRED_RNG); |
| 75 | + if (!BCRYPT_SUCCESS(status)) { |
| 76 | + fatalError(0, "Fatal error: 0x%lX in '%s'\n", status, __func__); |
| 77 | + } |
71 | 78 |
|
72 |
| - NTSTATUS status = BCryptGenRandom(nullptr, |
73 |
| - static_cast<PUCHAR>(buf), |
74 |
| - static_cast<ULONG>(nbytes), |
75 |
| - BCRYPT_USE_SYSTEM_PREFERRED_RNG); |
76 |
| - if (!BCRYPT_SUCCESS(status)) { |
77 |
| - fatalError(0, "Fatal error: 0x%lX in '%s'\n", status, __func__); |
| 79 | + buf = static_cast<uint8_t *>(buf) + actual_nbytes; |
| 80 | + nbytes -= actual_nbytes; |
78 | 81 | }
|
79 | 82 | }
|
80 | 83 |
|
|
0 commit comments