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, ...)
This commit is contained in:
Sébastien Marie
2021-03-02 08:09:51 +00:00
committed by Isaac Freund
parent e9a038c33b
commit 89e522b935
4 changed files with 24 additions and 18 deletions

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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),
}
}

View File

@@ -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);