authorgravatar for semarie@online.frSébastien Marie <semarie@online.fr> 2021-03-02 08:09:51+00:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-03-12 15:04:36+01:00
log89e522b935a8cca96b2e6d0cce0515a1eb8e6451
tree937a1ea13abd2cf8bae6b6f48f44230dd8ab5ae4
parente9a038c33bbf171695b08540536f307b9e418173

make std.c.getErrno() return same type as _errno() aka c_int

adjust std.os.unexpectedErrno() to be correct for all std.os.system.errno (c_int, u12, usize, ...)

4 files changed, 24 insertions(+), 18 deletions(-)

lib/std/Thread.zig+1-1
......@@ -362,7 +362,7 @@ pub fn spawn(comptime startFn: anytype, context: SpawnContextType(@TypeOf(startF
362362 os.EAGAIN => return error.SystemResources,
363363 os.EPERM => unreachable,
364364 os.EINVAL => unreachable,
365 else => return os.unexpectedErrno(@intCast(usize, err)),
365 else => return os.unexpectedErrno(err),
366366 }
367367
368368 return thread_obj;
lib/std/c.zig+2-2
......@@ -37,9 +37,9 @@ pub usingnamespace switch (std.Target.current.os.tag) {
3737 else => struct {},
3838};
3939
40pub fn getErrno(rc: anytype) u16 {
40pub fn getErrno(rc: anytype) c_int {
4141 if (rc == -1) {
42 return @intCast(u16, _errno().*);
42 return _errno().*;
4343 } else {
4444 return 0;
4545 }
lib/std/io/c_writer.zig+1-1
......@@ -30,7 +30,7 @@ fn cWriterWrite(c_file: *std.c.FILE, bytes: []const u8) std.fs.File.WriteError!u
3030 os.ENOSPC => return error.NoSpaceLeft,
3131 os.EPERM => return error.AccessDenied,
3232 os.EPIPE => return error.BrokenPipe,
33 else => |err| return os.unexpectedErrno(@intCast(usize, err)),
33 else => |err| return os.unexpectedErrno(err),
3434 }
3535}
3636
lib/std/os.zig+20-14
......@@ -144,25 +144,27 @@ pub fn getrandom(buffer: []u8) GetRandomError!void {
144144 std.c.versionCheck(builtin.Version{ .major = 2, .minor = 25, .patch = 0 }).ok;
145145
146146 while (buf.len != 0) {
147 var err: u16 = undefined;
148
149 const num_read = if (use_c) blk: {
147 const res = if (use_c) blk: {
150148 const rc = std.c.getrandom(buf.ptr, buf.len, 0);
151 err = std.c.getErrno(rc);
152 break :blk @bitCast(usize, rc);
149 break :blk .{
150 .num_read = @bitCast(usize, rc),
151 .err = std.c.getErrno(rc),
152 };
153153 } else blk: {
154154 const rc = linux.getrandom(buf.ptr, buf.len, 0);
155 err = linux.getErrno(rc);
156 break :blk rc;
155 break :blk .{
156 .num_read = rc,
157 .err = linux.getErrno(rc),
158 };
157159 };
158160
159 switch (err) {
160 0 => buf = buf[num_read..],
161 switch (res.err) {
162 0 => buf = buf[res.num_read..],
161163 EINVAL => unreachable,
162164 EFAULT => unreachable,
163165 EINTR => continue,
164166 ENOSYS => return getRandomBytesDevURandom(buf),
165 else => return unexpectedErrno(err),
167 else => return unexpectedErrno(res.err),
166168 }
167169 }
168170 return;
......@@ -1500,7 +1502,7 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {
15001502 EINVAL => unreachable,
15011503 ENOENT => return error.CurrentWorkingDirectoryUnlinked,
15021504 ERANGE => return error.NameTooLong,
1503 else => return unexpectedErrno(@intCast(usize, err)),
1505 else => return unexpectedErrno(err),
15041506 }
15051507}
15061508
......@@ -3661,7 +3663,7 @@ pub fn mmap(
36613663 const err = if (builtin.link_libc) blk: {
36623664 const rc = std.c.mmap(ptr, length, prot, flags, fd, offset);
36633665 if (rc != std.c.MAP_FAILED) return @ptrCast([*]align(mem.page_size) u8, @alignCast(mem.page_size, rc))[0..length];
3664 break :blk @intCast(usize, system._errno().*);
3666 break :blk system._errno().*;
36653667 } else blk: {
36663668 const rc = system.mmap(ptr, length, prot, flags, fd, offset);
36673669 const err = errno(rc);
......@@ -4321,7 +4323,7 @@ pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealP
43214323 ENAMETOOLONG => return error.NameTooLong,
43224324 ELOOP => return error.SymLinkLoop,
43234325 EIO => return error.InputOutput,
4324 else => |err| return unexpectedErrno(@intCast(usize, err)),
4326 else => |err| return unexpectedErrno(err),
43254327 };
43264328 return mem.spanZ(result_path);
43274329}
......@@ -4622,7 +4624,11 @@ pub const UnexpectedError = error{
46224624
46234625/// Call this when you made a syscall or something that sets errno
46244626/// and you get an unexpected error.
4625pub fn unexpectedErrno(err: usize) UnexpectedError {
4627pub fn unexpectedErrno(err: anytype) UnexpectedError {
4628 if (@typeInfo(@TypeOf(err)) != .Int) {
4629 @compileError("err is expected to be an integer");
4630 }
4631
46264632 if (unexpected_error_tracing) {
46274633 std.debug.warn("unexpected errno: {d}\n", .{err});
46284634 std.debug.dumpCurrentStackTrace(null);