diff --git a/lib/std/Io/Threaded.zig b/lib/std/Io/Threaded.zig index 385bade400..d265c5fdc9 100644 --- a/lib/std/Io/Threaded.zig +++ b/lib/std/Io/Threaded.zig @@ -9675,10 +9675,12 @@ fn fileWriteFileStreaming( file_reader.interface.toss(n -| header.len); return n; } + var len: usize = @intFromEnum(limit); var off_in: i64 = undefined; const off_in_ptr: ?*i64 = switch (file_reader.mode) { .positional_simple, .streaming_simple => return error.Unimplemented, .positional => p: { + len = @min(len, std.math.maxInt(usize) - file_reader.pos); off_in = @intCast(file_reader.pos); break :p &off_in; }, @@ -9689,7 +9691,7 @@ fn fileWriteFileStreaming( .linux => n: { const syscall: Syscall = try .start(); while (true) { - const rc = linux_copy_file_range_sys.copy_file_range(in_fd, off_in_ptr, out_fd, null, @intFromEnum(limit), 0); + const rc = linux_copy_file_range_sys.copy_file_range(in_fd, off_in_ptr, out_fd, null, len, 0); switch (linux_copy_file_range_sys.errno(rc)) { .SUCCESS => { syscall.finish(); @@ -9849,10 +9851,12 @@ fn fileWriteFilePositional( file_reader.interface.toss(n -| header.len); return n; } + var len: usize = @min(@intFromEnum(limit), std.math.maxInt(usize) - offset); var off_in: i64 = undefined; const off_in_ptr: ?*i64 = switch (file_reader.mode) { .positional_simple, .streaming_simple => return error.Unimplemented, .positional => p: { + len = @min(len, std.math.maxInt(usize) - file_reader.pos); off_in = @intCast(file_reader.pos); break :p &off_in; }, @@ -9864,7 +9868,7 @@ fn fileWriteFilePositional( .linux => n: { const syscall: Syscall = try .start(); while (true) { - const rc = linux_copy_file_range_sys.copy_file_range(in_fd, off_in_ptr, out_fd, &off_out, @intFromEnum(limit), 0); + const rc = linux_copy_file_range_sys.copy_file_range(in_fd, off_in_ptr, out_fd, &off_out, len, 0); switch (linux_copy_file_range_sys.errno(rc)) { .SUCCESS => { syscall.finish(); @@ -9888,7 +9892,7 @@ fn fileWriteFilePositional( .IO => return error.InputOutput, .NOMEM => return error.SystemResources, .NOSPC => return error.NoSpaceLeft, - .OVERFLOW => return error.Unseekable, + .OVERFLOW => |err| errnoBug(err), // We avoid passing too large a count. .NXIO => return error.Unseekable, .SPIPE => return error.Unseekable, .PERM => return error.PermissionDenied,