diff options
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/Thread.zig | 2 | ||||
| -rw-r--r-- | lib/std/c.zig | 4 | ||||
| -rw-r--r-- | lib/std/io/c_writer.zig | 2 | ||||
| -rw-r--r-- | lib/std/os.zig | 34 |
4 files changed, 24 insertions, 18 deletions
diff --git a/lib/std/Thread.zig b/lib/std/Thread.zig index b9a69d151f..7e8a6226e6 100644 --- a/lib/std/Thread.zig +++ b/lib/std/Thread.zig @@ -362,7 +362,7 @@ pub fn spawn(comptime startFn: anytype, context: SpawnContextType(@TypeOf(startF os.EAGAIN => return error.SystemResources, os.EPERM => unreachable, os.EINVAL => unreachable, - else => return os.unexpectedErrno(@intCast(usize, err)), + else => return os.unexpectedErrno(err), } return thread_obj; diff --git a/lib/std/c.zig b/lib/std/c.zig index 1688824dd9..bd0ce04f75 100644 --- a/lib/std/c.zig +++ b/lib/std/c.zig @@ -37,9 +37,9 @@ pub usingnamespace switch (std.Target.current.os.tag) { else => struct {}, }; -pub fn getErrno(rc: anytype) u16 { +pub fn getErrno(rc: anytype) c_int { if (rc == -1) { - return @intCast(u16, _errno().*); + return _errno().*; } else { return 0; } diff --git a/lib/std/io/c_writer.zig b/lib/std/io/c_writer.zig index fa7d7eb13a..26ae1fde01 100644 --- a/lib/std/io/c_writer.zig +++ b/lib/std/io/c_writer.zig @@ -30,7 +30,7 @@ fn cWriterWrite(c_file: *std.c.FILE, bytes: []const u8) std.fs.File.WriteError!u os.ENOSPC => return error.NoSpaceLeft, os.EPERM => return error.AccessDenied, os.EPIPE => return error.BrokenPipe, - else => |err| return os.unexpectedErrno(@intCast(usize, err)), + else => |err| return os.unexpectedErrno(err), } } diff --git a/lib/std/os.zig b/lib/std/os.zig index a2e62ed0ae..362a58f7fb 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -144,25 +144,27 @@ pub fn getrandom(buffer: []u8) GetRandomError!void { std.c.versionCheck(builtin.Version{ .major = 2, .minor = 25, .patch = 0 }).ok; while (buf.len != 0) { - var err: u16 = undefined; - - const num_read = if (use_c) blk: { + const res = if (use_c) blk: { const rc = std.c.getrandom(buf.ptr, buf.len, 0); - err = std.c.getErrno(rc); - break :blk @bitCast(usize, rc); + break :blk .{ + .num_read = @bitCast(usize, rc), + .err = std.c.getErrno(rc), + }; } else blk: { const rc = linux.getrandom(buf.ptr, buf.len, 0); - err = linux.getErrno(rc); - break :blk rc; + break :blk .{ + .num_read = rc, + .err = linux.getErrno(rc), + }; }; - switch (err) { - 0 => buf = buf[num_read..], + switch (res.err) { + 0 => buf = buf[res.num_read..], EINVAL => unreachable, EFAULT => unreachable, EINTR => continue, ENOSYS => return getRandomBytesDevURandom(buf), - else => return unexpectedErrno(err), + else => return unexpectedErrno(res.err), } } return; @@ -1500,7 +1502,7 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 { EINVAL => unreachable, ENOENT => return error.CurrentWorkingDirectoryUnlinked, ERANGE => return error.NameTooLong, - else => return unexpectedErrno(@intCast(usize, err)), + else => return unexpectedErrno(err), } } @@ -3661,7 +3663,7 @@ pub fn mmap( const err = if (builtin.link_libc) blk: { const rc = std.c.mmap(ptr, length, prot, flags, fd, offset); if (rc != std.c.MAP_FAILED) return @ptrCast([*]align(mem.page_size) u8, @alignCast(mem.page_size, rc))[0..length]; - break :blk @intCast(usize, system._errno().*); + break :blk system._errno().*; } else blk: { const rc = system.mmap(ptr, length, prot, flags, fd, offset); const err = errno(rc); @@ -4321,7 +4323,7 @@ pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealP ENAMETOOLONG => return error.NameTooLong, ELOOP => return error.SymLinkLoop, EIO => return error.InputOutput, - else => |err| return unexpectedErrno(@intCast(usize, err)), + else => |err| return unexpectedErrno(err), }; return mem.spanZ(result_path); } @@ -4622,7 +4624,11 @@ pub const UnexpectedError = error{ /// Call this when you made a syscall or something that sets errno /// and you get an unexpected error. -pub fn unexpectedErrno(err: usize) UnexpectedError { +pub fn unexpectedErrno(err: anytype) UnexpectedError { + if (@typeInfo(@TypeOf(err)) != .Int) { + @compileError("err is expected to be an integer"); + } + if (unexpected_error_tracing) { std.debug.warn("unexpected errno: {d}\n", .{err}); std.debug.dumpCurrentStackTrace(null); |
