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 {...@@ -658,6 +658,7 @@ pub const Request = struct {
658 MissingEndCertificateMarker,658 MissingEndCertificateMarker,
659 InvalidPadding,659 InvalidPadding,
660 EndOfStream,660 EndOfStream,
661 InvalidArgument,
661 };662 };
662663
663 pub fn read(req: *Request, buffer: []u8) ReadError!usize {664 pub fn read(req: *Request, buffer: []u8) ReadError!usize {
lib/std/os.zig+5-4
...@@ -1027,6 +1027,7 @@ pub const WriteError = error{...@@ -1027,6 +1027,7 @@ pub const WriteError = error{
1027 InputOutput,1027 InputOutput,
1028 NoSpaceLeft,1028 NoSpaceLeft,
1029 DeviceBusy,1029 DeviceBusy,
1030 InvalidArgument,
10301031
1031 /// In WASI, this error may occur when the file descriptor does1032 /// In WASI, this error may occur when the file descriptor does
1032 /// not hold the required rights to write to it.1033 /// not hold the required rights to write to it.
...@@ -1113,7 +1114,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {...@@ -1113,7 +1114,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
1113 switch (errno(rc)) {1114 switch (errno(rc)) {
1114 .SUCCESS => return @intCast(usize, rc),1115 .SUCCESS => return @intCast(usize, rc),
1115 .INTR => continue,1116 .INTR => continue,
1116 .INVAL => unreachable,1117 .INVAL => return error.InvalidArgument,
1117 .FAULT => unreachable,1118 .FAULT => unreachable,
1118 .AGAIN => return error.WouldBlock,1119 .AGAIN => return error.WouldBlock,
1119 .BADF => return error.NotOpenForWriting, // can be a race condition.1120 .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 {...@@ -1183,7 +1184,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {
1183 switch (errno(rc)) {1184 switch (errno(rc)) {
1184 .SUCCESS => return @intCast(usize, rc),1185 .SUCCESS => return @intCast(usize, rc),
1185 .INTR => continue,1186 .INTR => continue,
1186 .INVAL => unreachable,1187 .INVAL => return error.InvalidArgument,
1187 .FAULT => unreachable,1188 .FAULT => unreachable,
1188 .AGAIN => return error.WouldBlock,1189 .AGAIN => return error.WouldBlock,
1189 .BADF => return error.NotOpenForWriting, // Can be a race condition.1190 .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 {...@@ -1278,7 +1279,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
1278 switch (errno(rc)) {1279 switch (errno(rc)) {
1279 .SUCCESS => return @intCast(usize, rc),1280 .SUCCESS => return @intCast(usize, rc),
1280 .INTR => continue,1281 .INTR => continue,
1281 .INVAL => unreachable,1282 .INVAL => return error.InvalidArgument,
1282 .FAULT => unreachable,1283 .FAULT => unreachable,
1283 .AGAIN => return error.WouldBlock,1284 .AGAIN => return error.WouldBlock,
1284 .BADF => return error.NotOpenForWriting, // Can be a race condition.1285 .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...@@ -1368,7 +1369,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz
1368 switch (errno(rc)) {1369 switch (errno(rc)) {
1369 .SUCCESS => return @intCast(usize, rc),1370 .SUCCESS => return @intCast(usize, rc),
1370 .INTR => continue,1371 .INTR => continue,
1371 .INVAL => unreachable,1372 .INVAL => return error.InvalidArgument,
1372 .FAULT => unreachable,1373 .FAULT => unreachable,
1373 .AGAIN => return error.WouldBlock,1374 .AGAIN => return error.WouldBlock,
1374 .BADF => return error.NotOpenForWriting, // Can be a race condition.1375 .BADF => return error.NotOpenForWriting, // Can be a race condition.
src/link.zig+1
...@@ -461,6 +461,7 @@ pub const File = struct {...@@ -461,6 +461,7 @@ pub const File = struct {
461 LockViolation,461 LockViolation,
462 NetNameDeleted,462 NetNameDeleted,
463 DeviceBusy,463 DeviceBusy,
464 InvalidArgument,
464 };465 };
465466
466 /// Called from within the CodeGen to lower a local variable instantion as an unnamed467 /// 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{...@@ -4622,6 +4622,7 @@ const FmtError = error{
4622 ConnectionResetByPeer,4622 ConnectionResetByPeer,
4623 LockViolation,4623 LockViolation,
4624 NetNameDeleted,4624 NetNameDeleted,
4625 InvalidArgument,
4625} || fs.File.OpenError;4626} || fs.File.OpenError;
46264627
4627fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void {4628fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void {