authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-20 15:25:30-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-20 15:25:30-04:00
log9bf283c0851b39d6c54d1f5dfb5c2c55cd0a1ab0
tree44eeb261c01821f2a6d442ae01db83c7fbca671f
parent79354243e32b0e64f4dc93bb581ff1b3061e3861
signaturelock-open Commit is signed but in an unrecognized format.

fixups

* getrandom libc prototypes had the wrong return type * `total_read` local variable was unnecessary since the sub-slice buffer has a length * I was able to get rid of all the integer casts * the err == 0 check can be a switch case * add missing `return` statement

3 files changed, 28 insertions(+), 37 deletions(-)

std/c/freebsd.zig+1-1
...@@ -6,4 +6,4 @@ pub const _errno = __error;...@@ -6,4 +6,4 @@ pub const _errno = __error;
66
7pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize;7pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize;
8pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;8pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
9pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) c_int;9pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize;
std/c/linux.zig+1-1
...@@ -7,7 +7,7 @@ pub const _errno = __errno_location;...@@ -7,7 +7,7 @@ pub const _errno = __errno_location;
77
8pub const MAP_FAILED = @intToPtr(*c_void, maxInt(usize));8pub const MAP_FAILED = @intToPtr(*c_void, maxInt(usize));
99
10pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) c_int;10pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize;
11pub extern "c" fn sched_getaffinity(pid: c_int, size: usize, set: *cpu_set_t) c_int;11pub extern "c" fn sched_getaffinity(pid: c_int, size: usize, set: *cpu_set_t) c_int;
12pub extern "c" fn eventfd(initval: c_uint, flags: c_uint) c_int;12pub extern "c" fn eventfd(initval: c_uint, flags: c_uint) c_int;
13pub extern "c" fn epoll_ctl(epfd: fd_t, op: c_uint, fd: fd_t, event: ?*epoll_event) c_int;13pub extern "c" fn epoll_ctl(epfd: fd_t, op: c_uint, fd: fd_t, event: ?*epoll_event) c_int;
std/os.zig+26-35
...@@ -99,55 +99,46 @@ pub const GetRandomError = OpenError;...@@ -99,55 +99,46 @@ pub const GetRandomError = OpenError;
99/// When linking against libc, this calls the99/// When linking against libc, this calls the
100/// appropriate OS-specific library call. Otherwise it uses the zig standard100/// appropriate OS-specific library call. Otherwise it uses the zig standard
101/// library implementation.101/// library implementation.
102pub fn getrandom(buf: []u8) GetRandomError!void {102pub fn getrandom(buffer: []u8) GetRandomError!void {
103 if (windows.is_the_target) {103 if (windows.is_the_target) {
104 return windows.RtlGenRandom(buf);104 return windows.RtlGenRandom(buffer);
105 }105 }
106 if (linux.is_the_target or freebsd.is_the_target) {106 if (linux.is_the_target or freebsd.is_the_target) {
107 var buf_slice: []u8 = buf[0..];107 var buf = buffer;
108 var total_read: usize = 0;108 const use_c = !linux.is_the_target or
109 const use_c = (!linux.is_the_target) or std.c.versionCheck(builtin.Version{ .major = 2, .minor = 25, .patch = 0 }).ok;109 std.c.versionCheck(builtin.Version{ .major = 2, .minor = 25, .patch = 0 }).ok;
110110
111 while (total_read < buf.len) {111 while (buf.len != 0) {
112 var err: u16 = 0;112 var err: u16 = undefined;
113
114 const num_read: usize = if (use_c) blk: {
115 const res: c_int = std.c.getrandom(buf_slice.ptr, buf_slice.len, 0);
116
117 if (res == -1) {
118 err = @intCast(u16, std.c._errno().*);
119 break :blk 0;
120 } else {
121 break :blk @intCast(usize, res);
122 }
123 } else blk: {
124 const res: usize = linux.getrandom(buf_slice.ptr, buf_slice.len, 0);
125113
126 err = @intCast(u16, linux.getErrno(res));114 const num_read = if (use_c) blk: {
127 break :blk res;115 const rc = std.c.getrandom(buf.ptr, buf.len, 0);
116 err = std.c.getErrno(rc);
117 break :blk @bitCast(usize, rc);
118 } else blk: {
119 const rc = linux.getrandom(buf.ptr, buf.len, 0);
120 err = linux.getErrno(rc);
121 break :blk rc;
128 };122 };
129123
130 if (err != 0) {124 switch (err) {
131 switch (err) {125 0 => buf = buf[num_read..],
132 EINVAL => unreachable,126 EINVAL => unreachable,
133 EFAULT => unreachable,127 EFAULT => unreachable,
134 EINTR => continue,128 EINTR => continue,
135 ENOSYS => return getRandomBytesDevURandom(buf),129 ENOSYS => return getRandomBytesDevURandom(buf),
136 else => return unexpectedErrno(err),130 else => return unexpectedErrno(err),
137 }
138 } else {
139 total_read += num_read;
140 buf_slice = buf_slice[num_read..];
141 }131 }
142 }132 }
133 return;
143 }134 }
144 if (wasi.is_the_target) {135 if (wasi.is_the_target) {
145 switch (wasi.random_get(buf.ptr, buf.len)) {136 switch (wasi.random_get(buffer.ptr, buffer.len)) {
146 0 => return,137 0 => return,
147 else => |err| return unexpectedErrno(err),138 else => |err| return unexpectedErrno(err),
148 }139 }
149 }140 }
150 return getRandomBytesDevURandom(buf);141 return getRandomBytesDevURandom(buffer);
151}142}
152143
153fn getRandomBytesDevURandom(buf: []u8) !void {144fn getRandomBytesDevURandom(buf: []u8) !void {