authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-14 23:37:36-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-15 14:18:20-08:00
log2b01764d86d8c1b808a1ffb5913442e8f055762f
treecc382115461ea949b40f4cbdb8bb6912617461f6
parentcb37a5c1fbfd991c6c299b27f35bbbe0b2b8d8de

std: fix handling of EBADF error code

In the context of read/write it's ambiguous, means file was opened without read/write respectively.

6 files changed, 17 insertions(+), 18 deletions(-)

lib/std/Io/File.zig+4
...@@ -554,6 +554,8 @@ pub const ReadPositionalError = error{...@@ -554,6 +554,8 @@ pub const ReadPositionalError = error{
554 LockViolation,554 LockViolation,
555 /// This file cannot be read positionally.555 /// This file cannot be read positionally.
556 Unseekable,556 Unseekable,
557 /// File was not opened with read capability.
558 NotOpenForReading,
557} || Io.Cancelable || Io.UnexpectedError;559} || Io.Cancelable || Io.UnexpectedError;
558560
559/// Returns 0 on stream end or if `buffer` has no space available for data.561/// Returns 0 on stream end or if `buffer` has no space available for data.
...@@ -588,6 +590,8 @@ pub const WritePositionalError = error{...@@ -588,6 +590,8 @@ pub const WritePositionalError = error{
588 FileBusy,590 FileBusy,
589 /// This file cannot be written positionally.591 /// This file cannot be written positionally.
590 Unseekable,592 Unseekable,
593 /// File was not opened with write capability.
594 NotOpenForWriting,
591} || Io.Cancelable || Io.UnexpectedError;595} || Io.Cancelable || Io.UnexpectedError;
592596
593/// See also:597/// See also:
lib/std/Io/File/Reader.zig+2-1
...@@ -33,7 +33,8 @@ pub const Error = error{...@@ -33,7 +33,8 @@ pub const Error = error{
33 IsDir,33 IsDir,
34 BrokenPipe,34 BrokenPipe,
35 ConnectionResetByPeer,35 ConnectionResetByPeer,
36 Timeout,36 /// File was not opened with read capability.
37 NotOpenForReading,
37 SocketUnconnected,38 SocketUnconnected,
38 /// Non-blocking has been enabled, and reading from the file descriptor39 /// Non-blocking has been enabled, and reading from the file descriptor
39 /// would block.40 /// would block.
lib/std/Io/Threaded.zig+9-11
...@@ -7954,7 +7954,7 @@ fn fileReadStreamingPosix(userdata: ?*anyopaque, file: File, data: []const []u8)...@@ -7954,7 +7954,7 @@ fn fileReadStreamingPosix(userdata: ?*anyopaque, file: File, data: []const []u8)
7954 syscall.finish();7954 syscall.finish();
7955 return nread;7955 return nread;
7956 },7956 },
7957 .INTR => {7957 .INTR, .TIMEDOUT => {
7958 try syscall.checkCancel();7958 try syscall.checkCancel();
7959 continue;7959 continue;
7960 },7960 },
...@@ -7970,7 +7970,6 @@ fn fileReadStreamingPosix(userdata: ?*anyopaque, file: File, data: []const []u8)...@@ -7970,7 +7970,6 @@ fn fileReadStreamingPosix(userdata: ?*anyopaque, file: File, data: []const []u8)
7970 .NOMEM => return error.SystemResources,7970 .NOMEM => return error.SystemResources,
7971 .NOTCONN => return error.SocketUnconnected,7971 .NOTCONN => return error.SocketUnconnected,
7972 .CONNRESET => return error.ConnectionResetByPeer,7972 .CONNRESET => return error.ConnectionResetByPeer,
7973 .TIMEDOUT => return error.Timeout,
7974 .NOTCAPABLE => return error.AccessDenied,7973 .NOTCAPABLE => return error.AccessDenied,
7975 else => |err| return posix.unexpectedErrno(err),7974 else => |err| return posix.unexpectedErrno(err),
7976 }7975 }
...@@ -7987,7 +7986,7 @@ fn fileReadStreamingPosix(userdata: ?*anyopaque, file: File, data: []const []u8)...@@ -7987,7 +7986,7 @@ fn fileReadStreamingPosix(userdata: ?*anyopaque, file: File, data: []const []u8)
7987 syscall.finish();7986 syscall.finish();
7988 return @intCast(rc);7987 return @intCast(rc);
7989 },7988 },
7990 .INTR => {7989 .INTR, .TIMEDOUT => {
7991 try syscall.checkCancel();7990 try syscall.checkCancel();
7992 continue;7991 continue;
7993 },7992 },
...@@ -7997,9 +7996,9 @@ fn fileReadStreamingPosix(userdata: ?*anyopaque, file: File, data: []const []u8)...@@ -7997,9 +7996,9 @@ fn fileReadStreamingPosix(userdata: ?*anyopaque, file: File, data: []const []u8)
7997 .INVAL => |err| return errnoBug(err),7996 .INVAL => |err| return errnoBug(err),
7998 .FAULT => |err| return errnoBug(err),7997 .FAULT => |err| return errnoBug(err),
7999 .AGAIN => return error.WouldBlock,7998 .AGAIN => return error.WouldBlock,
8000 .BADF => |err| {7999 .BADF => {
8001 if (native_os == .wasi) return error.IsDir; // File operation on directory.8000 if (native_os == .wasi) return error.IsDir; // File operation on directory.
8002 return errnoBug(err); // File descriptor used after closed.8001 return error.NotOpenForReading;
8003 },8002 },
8004 .IO => return error.InputOutput,8003 .IO => return error.InputOutput,
8005 .ISDIR => return error.IsDir,8004 .ISDIR => return error.IsDir,
...@@ -8007,7 +8006,6 @@ fn fileReadStreamingPosix(userdata: ?*anyopaque, file: File, data: []const []u8)...@@ -8007,7 +8006,6 @@ fn fileReadStreamingPosix(userdata: ?*anyopaque, file: File, data: []const []u8)
8007 .NOMEM => return error.SystemResources,8006 .NOMEM => return error.SystemResources,
8008 .NOTCONN => return error.SocketUnconnected,8007 .NOTCONN => return error.SocketUnconnected,
8009 .CONNRESET => return error.ConnectionResetByPeer,8008 .CONNRESET => return error.ConnectionResetByPeer,
8010 .TIMEDOUT => return error.Timeout,
8011 else => |err| return posix.unexpectedErrno(err),8009 else => |err| return posix.unexpectedErrno(err),
8012 }8010 }
8013 },8011 },
...@@ -8136,10 +8134,10 @@ fn fileReadPositionalPosix(userdata: ?*anyopaque, file: File, data: []const []u8...@@ -8136,10 +8134,10 @@ fn fileReadPositionalPosix(userdata: ?*anyopaque, file: File, data: []const []u8
8136 .CONNRESET => |err| return syscall.errnoBug(err), // not a socket8134 .CONNRESET => |err| return syscall.errnoBug(err), // not a socket
8137 .INVAL => |err| return syscall.errnoBug(err),8135 .INVAL => |err| return syscall.errnoBug(err),
8138 .FAULT => |err| return syscall.errnoBug(err),8136 .FAULT => |err| return syscall.errnoBug(err),
8139 .BADF => |err| {8137 .BADF => {
8140 syscall.finish();8138 syscall.finish();
8141 if (native_os == .wasi) return error.IsDir; // File operation on directory.8139 if (native_os == .wasi) return error.IsDir; // File operation on directory.
8142 return errnoBug(err); // File descriptor used after closed.8140 return error.NotOpenForReading;
8143 },8141 },
8144 else => |err| return syscall.unexpectedErrno(err),8142 else => |err| return syscall.unexpectedErrno(err),
8145 }8143 }
...@@ -8793,7 +8791,7 @@ fn fileWritePositional(...@@ -8793,7 +8791,7 @@ fn fileWritePositional(
8793 .INVAL => |err| return errnoBug(err),8791 .INVAL => |err| return errnoBug(err),
8794 .FAULT => |err| return errnoBug(err),8792 .FAULT => |err| return errnoBug(err),
8795 .AGAIN => |err| return errnoBug(err),8793 .AGAIN => |err| return errnoBug(err),
8796 .BADF => |err| return errnoBug(err), // use after free8794 .BADF => return error.NotOpenForWriting,
8797 .DESTADDRREQ => |err| return errnoBug(err), // `connect` was never called.8795 .DESTADDRREQ => |err| return errnoBug(err), // `connect` was never called.
8798 .DQUOT => return error.DiskQuota,8796 .DQUOT => return error.DiskQuota,
8799 .FBIG => return error.FileTooBig,8797 .FBIG => return error.FileTooBig,
...@@ -8828,7 +8826,7 @@ fn fileWritePositional(...@@ -8828,7 +8826,7 @@ fn fileWritePositional(
8828 .FAULT => |err| return syscall.errnoBug(err),8826 .FAULT => |err| return syscall.errnoBug(err),
8829 .DESTADDRREQ => |err| return syscall.errnoBug(err), // `connect` was never called.8827 .DESTADDRREQ => |err| return syscall.errnoBug(err), // `connect` was never called.
8830 .CONNRESET => |err| return syscall.errnoBug(err), // Not a socket handle.8828 .CONNRESET => |err| return syscall.errnoBug(err), // Not a socket handle.
8831 .BADF => |err| return syscall.errnoBug(err), // use after free8829 .BADF => return syscall.fail(error.NotOpenForWriting),
8832 .AGAIN => return syscall.fail(error.WouldBlock),8830 .AGAIN => return syscall.fail(error.WouldBlock),
8833 .DQUOT => return syscall.fail(error.DiskQuota),8831 .DQUOT => return syscall.fail(error.DiskQuota),
8834 .FBIG => return syscall.fail(error.FileTooBig),8832 .FBIG => return syscall.fail(error.FileTooBig),
...@@ -16636,7 +16634,7 @@ fn mmSyncWrite(file: File, memory: []u8, offset: u64) File.WritePositionalError!...@@ -16636,7 +16634,7 @@ fn mmSyncWrite(file: File, memory: []u8, offset: u64) File.WritePositionalError!
16636 .FAULT => |err| return syscall.errnoBug(err),16634 .FAULT => |err| return syscall.errnoBug(err),
16637 .DESTADDRREQ => |err| return syscall.errnoBug(err), // not a socket16635 .DESTADDRREQ => |err| return syscall.errnoBug(err), // not a socket
16638 .CONNRESET => |err| return syscall.errnoBug(err), // not a socket16636 .CONNRESET => |err| return syscall.errnoBug(err), // not a socket
16639 .BADF => |err| return syscall.errnoBug(err), // use after free16637 .BADF => return syscall.fail(error.NotOpenForWriting),
16640 .AGAIN => return syscall.fail(error.WouldBlock),16638 .AGAIN => return syscall.fail(error.WouldBlock),
16641 .DQUOT => return syscall.fail(error.DiskQuota),16639 .DQUOT => return syscall.fail(error.DiskQuota),
16642 .FBIG => return syscall.fail(error.FileTooBig),16640 .FBIG => return syscall.fail(error.FileTooBig),
lib/std/posix.zig+1-1
...@@ -441,7 +441,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {...@@ -441,7 +441,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
441 .NOMEM => return error.SystemResources,441 .NOMEM => return error.SystemResources,
442 .NOTCONN => return error.SocketUnconnected,442 .NOTCONN => return error.SocketUnconnected,
443 .CONNRESET => return error.ConnectionResetByPeer,443 .CONNRESET => return error.ConnectionResetByPeer,
444 .TIMEDOUT => return error.Timeout,444 .TIMEDOUT => return error.Unexpected,
445 else => |err| return unexpectedErrno(err),445 else => |err| return unexpectedErrno(err),
446 }446 }
447 }447 }
lib/std/zig/system.zig+1-1
...@@ -420,7 +420,7 @@ pub fn resolveTargetQuery(io: Io, query: Target.Query) DetectError!Target {...@@ -420,7 +420,7 @@ pub fn resolveTargetQuery(io: Io, query: Target.Query) DetectError!Target {
420 error.WouldBlock => return error.Unexpected,420 error.WouldBlock => return error.Unexpected,
421 error.BrokenPipe => return error.Unexpected,421 error.BrokenPipe => return error.Unexpected,
422 error.ConnectionResetByPeer => return error.Unexpected,422 error.ConnectionResetByPeer => return error.Unexpected,
423 error.Timeout => return error.Unexpected,423 error.NotOpenForReading => return error.Unexpected,
424 error.SocketUnconnected => return error.Unexpected,424 error.SocketUnconnected => return error.Unexpected,
425425
426 error.AccessDenied,426 error.AccessDenied,
src/link/Dwarf.zig-4
...@@ -50,13 +50,9 @@ pub const UpdateError = error{...@@ -50,13 +50,9 @@ pub const UpdateError = error{
50 UnexpectedEndOfFile,50 UnexpectedEndOfFile,
51 NonResizable,51 NonResizable,
52 /// TODO why is this in the error set?52 /// TODO why is this in the error set?
53 Timeout,
54 /// TODO why is this in the error set?
55 ConnectionResetByPeer,53 ConnectionResetByPeer,
56 /// TODO why is this in the error set?54 /// TODO why is this in the error set?
57 SocketUnconnected,55 SocketUnconnected,
58 /// TODO why is this in the error set?
59 NotOpenForWriting,
60} ||56} ||
61 codegen.GenerateSymbolError ||57 codegen.GenerateSymbolError ||
62 Io.File.OpenError ||58 Io.File.OpenError ||