Skip to content

Commit 6ab04b5

Browse files
shadeopsandrewrk
authored andcommitted
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 commit 6ab04b5

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

lib/std/http/Client.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4622,6 +4622,7 @@ const FmtError = error{
46224622
ConnectionResetByPeer,
46234623
LockViolation,
46244624
NetNameDeleted,
4625+
InvalidArgument,
46254626
} || fs.File.OpenError;
46264627

46274628
fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void {

0 commit comments

Comments
 (0)