From 0d0f09fb0ee60b5fa42f51732bde2a4db43453a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Thu, 7 Aug 2025 17:28:37 +0200 Subject: [PATCH] std.os.windows: map RtlGenRandom() failure to error.SystemResources Closes #23666. --- lib/std/os/windows.zig | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig index a2d62f42ec..66583d21b2 100644 --- a/lib/std/os/windows.zig +++ b/lib/std/os/windows.zig @@ -408,7 +408,12 @@ pub fn SetHandleInformation(h: HANDLE, mask: DWORD, flags: DWORD) SetHandleInfor } } -pub const RtlGenRandomError = error{Unexpected}; +pub const RtlGenRandomError = error{ + /// `RtlGenRandom` has been known to fail in situations where the system is under heavy load. + /// Unfortunately, it does not call `SetLastError`, so it is not possible to get more specific + /// error information; it could actually be due to an out-of-memory condition, for example. + SystemResources, +}; /// Call RtlGenRandom() instead of CryptGetRandom() on Windows /// https://github.com/rust-lang-nursery/rand/issues/111 @@ -422,7 +427,7 @@ pub fn RtlGenRandom(output: []u8) RtlGenRandomError!void { const to_read: ULONG = @min(buff.len, max_read_size); if (advapi32.RtlGenRandom(buff.ptr, to_read) == 0) { - return unexpectedError(GetLastError()); + return error.SystemResources; } total_read += to_read;