Commit 6ab04b5941

jim price <shadeops@gmail.com>
2023-03-06 01:39:56
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.
1 parent 2770159
Changed files (4)
lib/std/http/Client.zig
@@ -658,6 +658,7 @@ pub const Request = struct {
         MissingEndCertificateMarker,
         InvalidPadding,
         EndOfStream,
+        InvalidArgument,
     };
 
     pub fn read(req: *Request, buffer: []u8) ReadError!usize {
lib/std/os.zig
@@ -1027,6 +1027,7 @@ pub const WriteError = error{
     InputOutput,
     NoSpaceLeft,
     DeviceBusy,
+    InvalidArgument,
 
     /// In WASI, this error may occur when the file descriptor does
     /// not hold the required rights to write to it.
@@ -1113,7 +1114,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
         switch (errno(rc)) {
             .SUCCESS => return @intCast(usize, rc),
             .INTR => continue,
-            .INVAL => unreachable,
+            .INVAL => return error.InvalidArgument,
             .FAULT => unreachable,
             .AGAIN => return error.WouldBlock,
             .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 {
         switch (errno(rc)) {
             .SUCCESS => return @intCast(usize, rc),
             .INTR => continue,
-            .INVAL => unreachable,
+            .INVAL => return error.InvalidArgument,
             .FAULT => unreachable,
             .AGAIN => return error.WouldBlock,
             .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 {
         switch (errno(rc)) {
             .SUCCESS => return @intCast(usize, rc),
             .INTR => continue,
-            .INVAL => unreachable,
+            .INVAL => return error.InvalidArgument,
             .FAULT => unreachable,
             .AGAIN => return error.WouldBlock,
             .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
         switch (errno(rc)) {
             .SUCCESS => return @intCast(usize, rc),
             .INTR => continue,
-            .INVAL => unreachable,
+            .INVAL => return error.InvalidArgument,
             .FAULT => unreachable,
             .AGAIN => return error.WouldBlock,
             .BADF => return error.NotOpenForWriting, // Can be a race condition.
src/link.zig
@@ -461,6 +461,7 @@ pub const File = struct {
         LockViolation,
         NetNameDeleted,
         DeviceBusy,
+        InvalidArgument,
     };
 
     /// Called from within the CodeGen to lower a local variable instantion as an unnamed
src/main.zig
@@ -4622,6 +4622,7 @@ const FmtError = error{
     ConnectionResetByPeer,
     LockViolation,
     NetNameDeleted,
+    InvalidArgument,
 } || fs.File.OpenError;
 
 fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void {