authorgravatar for git@l4.pmLuna <git@l4.pm> 2020-06-28 17:38:24-03:00
committergravatar for git@l4.pmLuna <git@l4.pm> 2020-06-28 18:40:43-03:00
logf47c6d3c6b5325ae682e50950765d352b2929126
treedc878c3eafb5cabf86620a4619547006883f7707
parent374e3e42e0de10d21406c077599cfc4a6a813497

std.os: make EBADF return error for read and write


5 files changed, 19 insertions(+), 9 deletions(-)

lib/std/elf.zig+1
...@@ -551,6 +551,7 @@ fn preadNoEof(file: std.fs.File, buf: []u8, offset: u64) !void {...@@ -551,6 +551,7 @@ fn preadNoEof(file: std.fs.File, buf: []u8, offset: u64) !void {
551 error.InputOutput => return error.FileSystem,551 error.InputOutput => return error.FileSystem,
552 error.Unexpected => return error.Unexpected,552 error.Unexpected => return error.Unexpected,
553 error.WouldBlock => return error.Unexpected,553 error.WouldBlock => return error.Unexpected,
554 error.NotOpenForReading => return error.Unexpected,
554 };555 };
555 if (len == 0) return error.UnexpectedEndOfFile;556 if (len == 0) return error.UnexpectedEndOfFile;
556 i += len;557 i += len;
lib/std/os.zig+11-9
...@@ -296,6 +296,7 @@ pub const ReadError = error{...@@ -296,6 +296,7 @@ pub const ReadError = error{
296 BrokenPipe,296 BrokenPipe,
297 ConnectionResetByPeer,297 ConnectionResetByPeer,
298 ConnectionTimedOut,298 ConnectionTimedOut,
299 NotOpenForReading,
299300
300 /// This error occurs when no global event loop is configured,301 /// This error occurs when no global event loop is configured,
301 /// and reading from the file descriptor would block.302 /// and reading from the file descriptor would block.
...@@ -359,7 +360,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {...@@ -359,7 +360,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
359 } else {360 } else {
360 return error.WouldBlock;361 return error.WouldBlock;
361 },362 },
362 EBADF => unreachable, // Always a race condition.363 EBADF => return error.NotOpenForReading, // Can be a race condition.
363 EIO => return error.InputOutput,364 EIO => return error.InputOutput,
364 EISDIR => return error.IsDir,365 EISDIR => return error.IsDir,
365 ENOBUFS => return error.SystemResources,366 ENOBUFS => return error.SystemResources,
...@@ -420,7 +421,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {...@@ -420,7 +421,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
420 } else {421 } else {
421 return error.WouldBlock;422 return error.WouldBlock;
422 },423 },
423 EBADF => unreachable, // always a race condition424 EBADF => return error.NotOpenForReading, // can be a race condition
424 EIO => return error.InputOutput,425 EIO => return error.InputOutput,
425 EISDIR => return error.IsDir,426 EISDIR => return error.IsDir,
426 ENOBUFS => return error.SystemResources,427 ENOBUFS => return error.SystemResources,
...@@ -483,7 +484,7 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {...@@ -483,7 +484,7 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
483 } else {484 } else {
484 return error.WouldBlock;485 return error.WouldBlock;
485 },486 },
486 EBADF => unreachable, // Always a race condition.487 EBADF => return error.NotOpenForReading, // Can be a race condition.
487 EIO => return error.InputOutput,488 EIO => return error.InputOutput,
488 EISDIR => return error.IsDir,489 EISDIR => return error.IsDir,
489 ENOBUFS => return error.SystemResources,490 ENOBUFS => return error.SystemResources,
...@@ -623,7 +624,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {...@@ -623,7 +624,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {
623 } else {624 } else {
624 return error.WouldBlock;625 return error.WouldBlock;
625 },626 },
626 EBADF => unreachable, // always a race condition627 EBADF => return error.NotOpenForReading, // can be a race condition
627 EIO => return error.InputOutput,628 EIO => return error.InputOutput,
628 EISDIR => return error.IsDir,629 EISDIR => return error.IsDir,
629 ENOBUFS => return error.SystemResources,630 ENOBUFS => return error.SystemResources,
...@@ -645,6 +646,7 @@ pub const WriteError = error{...@@ -645,6 +646,7 @@ pub const WriteError = error{
645 BrokenPipe,646 BrokenPipe,
646 SystemResources,647 SystemResources,
647 OperationAborted,648 OperationAborted,
649 NotOpenForWriting,
648650
649 /// This error occurs when no global event loop is configured,651 /// This error occurs when no global event loop is configured,
650 /// and reading from the file descriptor would block.652 /// and reading from the file descriptor would block.
...@@ -720,7 +722,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {...@@ -720,7 +722,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
720 } else {722 } else {
721 return error.WouldBlock;723 return error.WouldBlock;
722 },724 },
723 EBADF => unreachable, // Always a race condition.725 EBADF => return error.NotOpenForWriting, // can be a race condition.
724 EDESTADDRREQ => unreachable, // `connect` was never called.726 EDESTADDRREQ => unreachable, // `connect` was never called.
725 EDQUOT => return error.DiskQuota,727 EDQUOT => return error.DiskQuota,
726 EFBIG => return error.FileTooBig,728 EFBIG => return error.FileTooBig,
...@@ -792,7 +794,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {...@@ -792,7 +794,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {
792 } else {794 } else {
793 return error.WouldBlock;795 return error.WouldBlock;
794 },796 },
795 EBADF => unreachable, // Always a race condition.797 EBADF => return error.NotOpenForWriting, // Can be a race condition.
796 EDESTADDRREQ => unreachable, // `connect` was never called.798 EDESTADDRREQ => unreachable, // `connect` was never called.
797 EDQUOT => return error.DiskQuota,799 EDQUOT => return error.DiskQuota,
798 EFBIG => return error.FileTooBig,800 EFBIG => return error.FileTooBig,
...@@ -880,7 +882,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {...@@ -880,7 +882,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
880 } else {882 } else {
881 return error.WouldBlock;883 return error.WouldBlock;
882 },884 },
883 EBADF => unreachable, // Always a race condition.885 EBADF => return error.NotOpenForWriting, // Can be a race condition.
884 EDESTADDRREQ => unreachable, // `connect` was never called.886 EDESTADDRREQ => unreachable, // `connect` was never called.
885 EDQUOT => return error.DiskQuota,887 EDQUOT => return error.DiskQuota,
886 EFBIG => return error.FileTooBig,888 EFBIG => return error.FileTooBig,
...@@ -967,7 +969,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz...@@ -967,7 +969,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz
967 } else {969 } else {
968 return error.WouldBlock;970 return error.WouldBlock;
969 },971 },
970 EBADF => unreachable, // Always a race condition.972 EBADF => return error.NotOpenForWriting, // Can be a race condition.
971 EDESTADDRREQ => unreachable, // `connect` was never called.973 EDESTADDRREQ => unreachable, // `connect` was never called.
972 EDQUOT => return error.DiskQuota,974 EDQUOT => return error.DiskQuota,
973 EFBIG => return error.FileTooBig,975 EFBIG => return error.FileTooBig,
...@@ -1162,7 +1164,7 @@ pub fn dup2(old_fd: fd_t, new_fd: fd_t) !void {...@@ -1162,7 +1164,7 @@ pub fn dup2(old_fd: fd_t, new_fd: fd_t) !void {
1162 EBUSY, EINTR => continue,1164 EBUSY, EINTR => continue,
1163 EMFILE => return error.ProcessFdQuotaExceeded,1165 EMFILE => return error.ProcessFdQuotaExceeded,
1164 EINVAL => unreachable, // invalid parameters passed to dup21166 EINVAL => unreachable, // invalid parameters passed to dup2
1165 EBADF => unreachable, // always a race condition1167 EBADF => unreachable, // invalid file descriptor
1166 else => |err| return unexpectedErrno(err),1168 else => |err| return unexpectedErrno(err),
1167 }1169 }
1168 }1170 }
lib/std/zig/system.zig+1
...@@ -851,6 +851,7 @@ pub const NativeTargetInfo = struct {...@@ -851,6 +851,7 @@ pub const NativeTargetInfo = struct {
851 const len = file.pread(buf[i .. buf.len - i], offset + i) catch |err| switch (err) {851 const len = file.pread(buf[i .. buf.len - i], offset + i) catch |err| switch (err) {
852 error.OperationAborted => unreachable, // Windows-only852 error.OperationAborted => unreachable, // Windows-only
853 error.WouldBlock => unreachable, // Did not request blocking mode853 error.WouldBlock => unreachable, // Did not request blocking mode
854 error.NotOpenForReading => unreachable,
854 error.SystemResources => return error.SystemResources,855 error.SystemResources => return error.SystemResources,
855 error.IsDir => return error.UnableToReadElfFile,856 error.IsDir => return error.UnableToReadElfFile,
856 error.BrokenPipe => return error.UnableToReadElfFile,857 error.BrokenPipe => return error.UnableToReadElfFile,
src-self-hosted/main.zig+2
...@@ -696,6 +696,7 @@ const FmtError = error{...@@ -696,6 +696,7 @@ const FmtError = error{
696 LinkQuotaExceeded,696 LinkQuotaExceeded,
697 FileBusy,697 FileBusy,
698 EndOfStream,698 EndOfStream,
699 NotOpenForWriting,
699} || fs.File.OpenError;700} || fs.File.OpenError;
700701
701fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void {702fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void {
...@@ -761,6 +762,7 @@ fn fmtPathFile(...@@ -761,6 +762,7 @@ fn fmtPathFile(
761 const source_code = source_file.readAllAlloc(fmt.gpa, stat.size, max_src_size) catch |err| switch (err) {762 const source_code = source_file.readAllAlloc(fmt.gpa, stat.size, max_src_size) catch |err| switch (err) {
762 error.ConnectionResetByPeer => unreachable,763 error.ConnectionResetByPeer => unreachable,
763 error.ConnectionTimedOut => unreachable,764 error.ConnectionTimedOut => unreachable,
765 error.NotOpenForReading => unreachable,
764 else => |e| return e,766 else => |e| return e,
765 };767 };
766 source_file.close();768 source_file.close();
src-self-hosted/stage2.zig+4
...@@ -153,6 +153,7 @@ export fn stage2_render_ast(tree: *ast.Tree, output_file: *FILE) Error {...@@ -153,6 +153,7 @@ export fn stage2_render_ast(tree: *ast.Tree, output_file: *FILE) Error {
153 const c_out_stream = std.io.cOutStream(output_file);153 const c_out_stream = std.io.cOutStream(output_file);
154 _ = std.zig.render(std.heap.c_allocator, c_out_stream, tree) catch |e| switch (e) {154 _ = std.zig.render(std.heap.c_allocator, c_out_stream, tree) catch |e| switch (e) {
155 error.WouldBlock => unreachable, // stage1 opens stuff in exclusively blocking mode155 error.WouldBlock => unreachable, // stage1 opens stuff in exclusively blocking mode
156 error.NotOpenForWriting => unreachable,
156 error.SystemResources => return .SystemResources,157 error.SystemResources => return .SystemResources,
157 error.OperationAborted => return .OperationAborted,158 error.OperationAborted => return .OperationAborted,
158 error.BrokenPipe => return .BrokenPipe,159 error.BrokenPipe => return .BrokenPipe,
...@@ -585,6 +586,8 @@ export fn stage2_libc_parse(stage1_libc: *Stage2LibCInstallation, libc_file_z: [...@@ -585,6 +586,8 @@ export fn stage2_libc_parse(stage1_libc: *Stage2LibCInstallation, libc_file_z: [
585 error.SystemResources => return .SystemResources,586 error.SystemResources => return .SystemResources,
586 error.OperationAborted => return .OperationAborted,587 error.OperationAborted => return .OperationAborted,
587 error.WouldBlock => unreachable,588 error.WouldBlock => unreachable,
589 error.NotOpenForWriting => unreachable,
590 error.NotOpenForReading => unreachable,
588 error.Unexpected => return .Unexpected,591 error.Unexpected => return .Unexpected,
589 error.EndOfStream => return .EndOfFile,592 error.EndOfStream => return .EndOfFile,
590 error.IsDir => return .IsDir,593 error.IsDir => return .IsDir,
...@@ -640,6 +643,7 @@ export fn stage2_libc_render(stage1_libc: *Stage2LibCInstallation, output_file:...@@ -640,6 +643,7 @@ export fn stage2_libc_render(stage1_libc: *Stage2LibCInstallation, output_file:
640 const c_out_stream = std.io.cOutStream(output_file);643 const c_out_stream = std.io.cOutStream(output_file);
641 libc.render(c_out_stream) catch |err| switch (err) {644 libc.render(c_out_stream) catch |err| switch (err) {
642 error.WouldBlock => unreachable, // stage1 opens stuff in exclusively blocking mode645 error.WouldBlock => unreachable, // stage1 opens stuff in exclusively blocking mode
646 error.NotOpenForWriting => unreachable,
643 error.SystemResources => return .SystemResources,647 error.SystemResources => return .SystemResources,
644 error.OperationAborted => return .OperationAborted,648 error.OperationAborted => return .OperationAborted,
645 error.BrokenPipe => return .BrokenPipe,649 error.BrokenPipe => return .BrokenPipe,