authorgravatar for devnexen@gmail.comDavid Carlier <devnexen@gmail.com> 2023-04-16 15:49:42+01:00
committergravatar for devnexen@gmail.comDavid Carlier <devnexen@gmail.com> 2023-04-22 21:01:12+01:00
log6248ac535b9bfc7113101c1ff0f0bb1e41c394d4
tree007eb8bef56e961e94c8e2cdcd6f0ef264c05fac
parentc579a23c5d997964a67883db5677c290bdbd7da6

os: getrandom wrapper favoring it for macOs/iOs only


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

lib/std/os.zig+13-2
......@@ -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 }