authorgravatar for shadeops@gmail.comjim price <shadeops@gmail.com> 2023-03-05 16:39:56-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-06 15:59:18-05:00
log6ab04b59417eb9c7fff81ea26424c2340dc72097
tree95a929045abb767ad47351af05778fb1ff0a6d32
parent27701596060c7a8018f9a9add21952a6d7f83d96

std.os: Allow write functions to return INVAL errors

In Linux when interacting with the virtual file system when writing in invalid value to a file the OS will return errno 22 (INVAL). Instead of triggering an unreachable, this change now returns a newly introduced error.InvalidArgument.

4 files changed, 8 insertions(+), 4 deletions(-)

lib/std/http/Client.zig+1
......@@ -658,6 +658,7 @@ pub const Request = struct {
658658 MissingEndCertificateMarker,
659659 InvalidPadding,
660660 EndOfStream,
661 InvalidArgument,
661662 };
662663
663664 pub fn read(req: *Request, buffer: []u8) ReadError!usize {
lib/std/os.zig+5-4
......@@ -1027,6 +1027,7 @@ pub const WriteError = error{
10271027 InputOutput,
10281028 NoSpaceLeft,
10291029 DeviceBusy,
1030 InvalidArgument,
10301031
10311032 /// In WASI, this error may occur when the file descriptor does
10321033 /// not hold the required rights to write to it.
......@@ -1113,7 +1114,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
11131114 switch (errno(rc)) {
11141115 .SUCCESS => return @intCast(usize, rc),
11151116 .INTR => continue,
1116 .INVAL => unreachable,
1117 .INVAL => return error.InvalidArgument,
11171118 .FAULT => unreachable,
11181119 .AGAIN => return error.WouldBlock,
11191120 .BADF => return error.NotOpenForWriting, // can be a race condition.
......@@ -1183,7 +1184,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {
11831184 switch (errno(rc)) {
11841185 .SUCCESS => return @intCast(usize, rc),
11851186 .INTR => continue,
1186 .INVAL => unreachable,
1187 .INVAL => return error.InvalidArgument,
11871188 .FAULT => unreachable,
11881189 .AGAIN => return error.WouldBlock,
11891190 .BADF => return error.NotOpenForWriting, // Can be a race condition.
......@@ -1278,7 +1279,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
12781279 switch (errno(rc)) {
12791280 .SUCCESS => return @intCast(usize, rc),
12801281 .INTR => continue,
1281 .INVAL => unreachable,
1282 .INVAL => return error.InvalidArgument,
12821283 .FAULT => unreachable,
12831284 .AGAIN => return error.WouldBlock,
12841285 .BADF => return error.NotOpenForWriting, // Can be a race condition.
......@@ -1368,7 +1369,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz
13681369 switch (errno(rc)) {
13691370 .SUCCESS => return @intCast(usize, rc),
13701371 .INTR => continue,
1371 .INVAL => unreachable,
1372 .INVAL => return error.InvalidArgument,
13721373 .FAULT => unreachable,
13731374 .AGAIN => return error.WouldBlock,
13741375 .BADF => return error.NotOpenForWriting, // Can be a race condition.
src/link.zig+1
......@@ -461,6 +461,7 @@ pub const File = struct {
461461 LockViolation,
462462 NetNameDeleted,
463463 DeviceBusy,
464 InvalidArgument,
464465 };
465466
466467 /// Called from within the CodeGen to lower a local variable instantion as an unnamed
src/main.zig+1
......@@ -4622,6 +4622,7 @@ const FmtError = error{
46224622 ConnectionResetByPeer,
46234623 LockViolation,
46244624 NetNameDeleted,
4625 InvalidArgument,
46254626} || fs.File.OpenError;
46264627
46274628fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void {