commit 0d0f09fb0ee60b5fa42f51732bde2a4db43453a8 (tree)
parent 3fb86841cc65437c65a6d599117833e260ea797c
Author: Alex Rønne Petersen <alex@alexrp.com>
Date: Thu, 7 Aug 2025 17:28:37 +0200
std.os.windows: map RtlGenRandom() failure to error.SystemResources
Closes #23666.
Diffstat:
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git 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;