Skip to content

Commit 0d0f09f

Browse files
committed
std.os.windows: map RtlGenRandom() failure to error.SystemResources
Closes ziglang#23666.
1 parent 3fb8684 commit 0d0f09f

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

lib/std/os/windows.zig

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,12 @@ pub fn SetHandleInformation(h: HANDLE, mask: DWORD, flags: DWORD) SetHandleInfor
408408
}
409409
}
410410

411-
pub const RtlGenRandomError = error{Unexpected};
411+
pub 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

413418
/// Call RtlGenRandom() instead of CryptGetRandom() on Windows
414419
/// https://github.com/rust-lang-nursery/rand/issues/111
@@ -422,7 +427,7 @@ pub fn RtlGenRandom(output: []u8) RtlGenRandomError!void {
422427
const to_read: ULONG = @min(buff.len, max_read_size);
423428

424429
if (advapi32.RtlGenRandom(buff.ptr, to_read) == 0) {
425-
return unexpectedError(GetLastError());
430+
return error.SystemResources;
426431
}
427432

428433
total_read += to_read;

0 commit comments

Comments
 (0)