Skip to content

Commit 6248ac5

Browse files
committed
os: getrandom wrapper favoring it for macOs/iOs only
1 parent c579a23 commit 6248ac5

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

lib/std/os.zig

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,18 @@ pub fn getrandom(buffer: []u8) GetRandomError!void {
512512
return;
513513
}
514514
switch (builtin.os.tag) {
515-
.netbsd, .openbsd, .macos, .ios, .tvos, .watchos => {
515+
.macos, .ios => {
516+
const rc = darwin.CCRandomGenerateBytes(buffer.ptr, buffer.len);
517+
if (rc != darwin.CCRNGStatus.kCCSuccess) {
518+
if (rc == darwin.CCRNGStatus.kCCParamError or rc == darwin.CCRNGStatus.kCCBufferTooSmall) {
519+
return error.InvalidHandle;
520+
} else {
521+
return error.SystemResources;
522+
}
523+
}
524+
return;
525+
},
526+
.netbsd, .openbsd, .tvos, .watchos => {
516527
system.arc4random_buf(buffer.ptr, buffer.len);
517528
return;
518529
},
@@ -991,7 +1002,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {
9911002
if (have_pread_but_not_preadv) {
9921003
// We could loop here; but proper usage of `preadv` must handle partial reads anyway.
9931004
// So we simply read into the first vector only.
994-
if (iov.len == 0) return @as(usize, 0);
1005+
if (iov.len == 0) return @intCast(usize, 0);
9951006
const first = iov[0];
9961007
return pread(fd, first.iov_base[0..first.iov_len], offset);
9971008
}

0 commit comments

Comments
 (0)