authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-08-07 17:28:37+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-08-08 07:25:26+02:00
log0d0f09fb0ee60b5fa42f51732bde2a4db43453a8
tree8ef542e4e9b9416da4d9727d51479a1835629eb4
parent3fb86841cc65437c65a6d599117833e260ea797c

std.os.windows: map RtlGenRandom() failure to error.SystemResources

Closes #23666.

1 files changed, 7 insertions(+), 2 deletions(-)

lib/std/os/windows.zig+7-2
...@@ -408,7 +408,12 @@ pub fn SetHandleInformation(h: HANDLE, mask: DWORD, flags: DWORD) SetHandleInfor...@@ -408,7 +408,12 @@ pub fn SetHandleInformation(h: HANDLE, mask: DWORD, flags: DWORD) SetHandleInfor
408 }408 }
409}409}
410410
411pub const RtlGenRandomError = error{Unexpected};411pub const RtlGenRandomError = error{
412 /// `RtlGenRandom` has been known to fail in situations where the system is under heavy load.
413 /// Unfortunately, it does not call `SetLastError`, so it is not possible to get more specific
414 /// error information; it could actually be due to an out-of-memory condition, for example.
415 SystemResources,
416};
412417
413/// Call RtlGenRandom() instead of CryptGetRandom() on Windows418/// Call RtlGenRandom() instead of CryptGetRandom() on Windows
414/// https://github.com/rust-lang-nursery/rand/issues/111419/// https://github.com/rust-lang-nursery/rand/issues/111
...@@ -422,7 +427,7 @@ pub fn RtlGenRandom(output: []u8) RtlGenRandomError!void {...@@ -422,7 +427,7 @@ pub fn RtlGenRandom(output: []u8) RtlGenRandomError!void {
422 const to_read: ULONG = @min(buff.len, max_read_size);427 const to_read: ULONG = @min(buff.len, max_read_size);
423428
424 if (advapi32.RtlGenRandom(buff.ptr, to_read) == 0) {429 if (advapi32.RtlGenRandom(buff.ptr, to_read) == 0) {
425 return unexpectedError(GetLastError());430 return error.SystemResources;
426 }431 }
427432
428 total_read += to_read;433 total_read += to_read;