@@ -65,16 +65,18 @@ void swift_stdlib_random(void *buf, __swift_size_t nbytes) {
65
65
66
66
SWIFT_RUNTIME_STDLIB_API
67
67
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
- }
68
+ while (nbytes > 0 ) {
69
+ auto actual_nbytes = std::min (nbytes, (__swift_size_t )ULONG_MAX);
70
+ NTSTATUS status = BCryptGenRandom (nullptr ,
71
+ static_cast <PUCHAR>(buf),
72
+ static_cast <ULONG>(actual_nbytes),
73
+ BCRYPT_USE_SYSTEM_PREFERRED_RNG);
74
+ if (!BCRYPT_SUCCESS (status)) {
75
+ fatalError (0 , " Fatal error: 0x%lX in '%s'\n " , status, __func__);
76
+ }
71
77
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__);
78
+ buf = static_cast <uint8_t *>(buf) + actual_nbytes;
79
+ nbytes -= actual_nbytes;
78
80
}
79
81
}
80
82
0 commit comments