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.
This commit is contained in:
jim price 2023-03-05 16:39:56 -08:00 committed by Andrew Kelley
parent 2770159606
commit 6ab04b5941
4 changed files with 8 additions and 4 deletions

View file

@ -658,6 +658,7 @@ pub const Request = struct {
MissingEndCertificateMarker, MissingEndCertificateMarker,
InvalidPadding, InvalidPadding,
EndOfStream, EndOfStream,
InvalidArgument,
}; };
pub fn read(req: *Request, buffer: []u8) ReadError!usize { pub fn read(req: *Request, buffer: []u8) ReadError!usize {

View file

@ -1027,6 +1027,7 @@ pub const WriteError = error{
InputOutput, InputOutput,
NoSpaceLeft, NoSpaceLeft,
DeviceBusy, DeviceBusy,
InvalidArgument,
/// In WASI, this error may occur when the file descriptor does /// In WASI, this error may occur when the file descriptor does
/// not hold the required rights to write to it. /// 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)) { switch (errno(rc)) {
.SUCCESS => return @intCast(usize, rc), .SUCCESS => return @intCast(usize, rc),
.INTR => continue, .INTR => continue,
.INVAL => unreachable, .INVAL => return error.InvalidArgument,
.FAULT => unreachable, .FAULT => unreachable,
.AGAIN => return error.WouldBlock, .AGAIN => return error.WouldBlock,
.BADF => return error.NotOpenForWriting, // can be a race condition. .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)) { switch (errno(rc)) {
.SUCCESS => return @intCast(usize, rc), .SUCCESS => return @intCast(usize, rc),
.INTR => continue, .INTR => continue,
.INVAL => unreachable, .INVAL => return error.InvalidArgument,
.FAULT => unreachable, .FAULT => unreachable,
.AGAIN => return error.WouldBlock, .AGAIN => return error.WouldBlock,
.BADF => return error.NotOpenForWriting, // Can be a race condition. .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)) { switch (errno(rc)) {
.SUCCESS => return @intCast(usize, rc), .SUCCESS => return @intCast(usize, rc),
.INTR => continue, .INTR => continue,
.INVAL => unreachable, .INVAL => return error.InvalidArgument,
.FAULT => unreachable, .FAULT => unreachable,
.AGAIN => return error.WouldBlock, .AGAIN => return error.WouldBlock,
.BADF => return error.NotOpenForWriting, // Can be a race condition. .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)) { switch (errno(rc)) {
.SUCCESS => return @intCast(usize, rc), .SUCCESS => return @intCast(usize, rc),
.INTR => continue, .INTR => continue,
.INVAL => unreachable, .INVAL => return error.InvalidArgument,
.FAULT => unreachable, .FAULT => unreachable,
.AGAIN => return error.WouldBlock, .AGAIN => return error.WouldBlock,
.BADF => return error.NotOpenForWriting, // Can be a race condition. .BADF => return error.NotOpenForWriting, // Can be a race condition.

View file

@ -461,6 +461,7 @@ pub const File = struct {
LockViolation, LockViolation,
NetNameDeleted, NetNameDeleted,
DeviceBusy, DeviceBusy,
InvalidArgument,
}; };
/// Called from within the CodeGen to lower a local variable instantion as an unnamed /// Called from within the CodeGen to lower a local variable instantion as an unnamed

View file

@ -4622,6 +4622,7 @@ const FmtError = error{
ConnectionResetByPeer, ConnectionResetByPeer,
LockViolation, LockViolation,
NetNameDeleted, NetNameDeleted,
InvalidArgument,
} || fs.File.OpenError; } || fs.File.OpenError;
fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void { fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void {