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...@@ -362,7 +362,7 @@ pub fn spawn(comptime startFn: anytype, context: SpawnContextType(@TypeOf(startF
362 os.EAGAIN => return error.SystemResources,362 os.EAGAIN => return error.SystemResources,
363 os.EPERM => unreachable,363 os.EPERM => unreachable,
364 os.EINVAL => unreachable,364 os.EINVAL => unreachable,
365 else => return os.unexpectedErrno(@intCast(usize, err)),365 else => return os.unexpectedErrno(err),
366 }366 }
367367
368 return thread_obj;368 return thread_obj;
lib/std/c.zig+2-2
...@@ -37,9 +37,9 @@ pub usingnamespace switch (std.Target.current.os.tag) {...@@ -37,9 +37,9 @@ pub usingnamespace switch (std.Target.current.os.tag) {
37 else => struct {},37 else => struct {},
38};38};
3939
40pub fn getErrno(rc: anytype) u16 {40pub fn getErrno(rc: anytype) c_int {
41 if (rc == -1) {41 if (rc == -1) {
42 return @intCast(u16, _errno().*);42 return _errno().*;
43 } else {43 } else {
44 return 0;44 return 0;
45 }45 }
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...@@ -30,7 +30,7 @@ fn cWriterWrite(c_file: *std.c.FILE, bytes: []const u8) std.fs.File.WriteError!u
30 os.ENOSPC => return error.NoSpaceLeft,30 os.ENOSPC => return error.NoSpaceLeft,
31 os.EPERM => return error.AccessDenied,31 os.EPERM => return error.AccessDenied,
32 os.EPIPE => return error.BrokenPipe,32 os.EPIPE => return error.BrokenPipe,
33 else => |err| return os.unexpectedErrno(@intCast(usize, err)),33 else => |err| return os.unexpectedErrno(err),
34 }34 }
35}35}
3636
lib/std/os.zig+20-14
...@@ -144,25 +144,27 @@ pub fn getrandom(buffer: []u8) GetRandomError!void {...@@ -144,25 +144,27 @@ pub fn getrandom(buffer: []u8) GetRandomError!void {
144 std.c.versionCheck(builtin.Version{ .major = 2, .minor = 25, .patch = 0 }).ok;144 std.c.versionCheck(builtin.Version{ .major = 2, .minor = 25, .patch = 0 }).ok;
145145
146 while (buf.len != 0) {146 while (buf.len != 0) {
147 var err: u16 = undefined;147 const res = if (use_c) blk: {
148
149 const num_read = if (use_c) blk: {
150 const rc = std.c.getrandom(buf.ptr, buf.len, 0);148 const rc = std.c.getrandom(buf.ptr, buf.len, 0);
151 err = std.c.getErrno(rc);149 break :blk .{
152 break :blk @bitCast(usize, rc);150 .num_read = @bitCast(usize, rc),
151 .err = std.c.getErrno(rc),
152 };
153 } else blk: {153 } else blk: {
154 const rc = linux.getrandom(buf.ptr, buf.len, 0);154 const rc = linux.getrandom(buf.ptr, buf.len, 0);
155 err = linux.getErrno(rc);155 break :blk .{
156 break :blk rc;156 .num_read = rc,
157 .err = linux.getErrno(rc),
158 };
157 };159 };
158160
159 switch (err) {161 switch (res.err) {
160 0 => buf = buf[num_read..],162 0 => buf = buf[res.num_read..],
161 EINVAL => unreachable,163 EINVAL => unreachable,
162 EFAULT => unreachable,164 EFAULT => unreachable,
163 EINTR => continue,165 EINTR => continue,
164 ENOSYS => return getRandomBytesDevURandom(buf),166 ENOSYS => return getRandomBytesDevURandom(buf),
165 else => return unexpectedErrno(err),167 else => return unexpectedErrno(res.err),
166 }168 }
167 }169 }
168 return;170 return;
...@@ -1500,7 +1502,7 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {...@@ -1500,7 +1502,7 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {
1500 EINVAL => unreachable,1502 EINVAL => unreachable,
1501 ENOENT => return error.CurrentWorkingDirectoryUnlinked,1503 ENOENT => return error.CurrentWorkingDirectoryUnlinked,
1502 ERANGE => return error.NameTooLong,1504 ERANGE => return error.NameTooLong,
1503 else => return unexpectedErrno(@intCast(usize, err)),1505 else => return unexpectedErrno(err),
1504 }1506 }
1505}1507}
15061508
...@@ -3661,7 +3663,7 @@ pub fn mmap(...@@ -3661,7 +3663,7 @@ pub fn mmap(
3661 const err = if (builtin.link_libc) blk: {3663 const err = if (builtin.link_libc) blk: {
3662 const rc = std.c.mmap(ptr, length, prot, flags, fd, offset);3664 const rc = std.c.mmap(ptr, length, prot, flags, fd, offset);
3663 if (rc != std.c.MAP_FAILED) return @ptrCast([*]align(mem.page_size) u8, @alignCast(mem.page_size, rc))[0..length];3665 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().*;
3665 } else blk: {3667 } else blk: {
3666 const rc = system.mmap(ptr, length, prot, flags, fd, offset);3668 const rc = system.mmap(ptr, length, prot, flags, fd, offset);
3667 const err = errno(rc);3669 const err = errno(rc);
...@@ -4321,7 +4323,7 @@ pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealP...@@ -4321,7 +4323,7 @@ pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealP
4321 ENAMETOOLONG => return error.NameTooLong,4323 ENAMETOOLONG => return error.NameTooLong,
4322 ELOOP => return error.SymLinkLoop,4324 ELOOP => return error.SymLinkLoop,
4323 EIO => return error.InputOutput,4325 EIO => return error.InputOutput,
4324 else => |err| return unexpectedErrno(@intCast(usize, err)),4326 else => |err| return unexpectedErrno(err),
4325 };4327 };
4326 return mem.spanZ(result_path);4328 return mem.spanZ(result_path);
4327}4329}
...@@ -4622,7 +4624,11 @@ pub const UnexpectedError = error{...@@ -4622,7 +4624,11 @@ pub const UnexpectedError = error{
46224624
4623/// Call this when you made a syscall or something that sets errno4625/// Call this when you made a syscall or something that sets errno
4624/// and you get an unexpected error.4626/// 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
4626 if (unexpected_error_tracing) {4632 if (unexpected_error_tracing) {
4627 std.debug.warn("unexpected errno: {d}\n", .{err});4633 std.debug.warn("unexpected errno: {d}\n", .{err});
4628 std.debug.dumpCurrentStackTrace(null);4634 std.debug.dumpCurrentStackTrace(null);