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 {...@@ -512,7 +512,18 @@ pub fn getrandom(buffer: []u8) GetRandomError!void {
512 return;512 return;
513 }513 }
514 switch (builtin.os.tag) {514 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 => {
516 system.arc4random_buf(buffer.ptr, buffer.len);527 system.arc4random_buf(buffer.ptr, buffer.len);
517 return;528 return;
518 },529 },
...@@ -991,7 +1002,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {...@@ -991,7 +1002,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {
991 if (have_pread_but_not_preadv) {1002 if (have_pread_but_not_preadv) {
992 // We could loop here; but proper usage of `preadv` must handle partial reads anyway.1003 // We could loop here; but proper usage of `preadv` must handle partial reads anyway.
993 // So we simply read into the first vector only.1004 // 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);
995 const first = iov[0];1006 const first = iov[0];
996 return pread(fd, first.iov_base[0..first.iov_len], offset);1007 return pread(fd, first.iov_base[0..first.iov_len], offset);
997 }1008 }