authorgravatar for rpkak@noreply.codeberg.orgrpkak <rpkak@noreply.codeberg.org> 2026-01-29 23:28:20+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-30 08:19:50+01:00
logb9819fce69e0f208e9e20071071a40863fbdb8a9
tree15842468312224bba89ec2b5488d980c4a0f536d
parent380ea6fb5eea659200d496540efea1fa4e39dcaf

Io.Threaded: limit copy_file_range len to prevent EOVERFLOW


1 files changed, 7 insertions(+), 3 deletions(-)

lib/std/Io/Threaded.zig+7-3
...@@ -9675,10 +9675,12 @@ fn fileWriteFileStreaming(...@@ -9675,10 +9675,12 @@ fn fileWriteFileStreaming(
9675 file_reader.interface.toss(n -| header.len);9675 file_reader.interface.toss(n -| header.len);
9676 return n;9676 return n;
9677 }9677 }
9678 var len: usize = @intFromEnum(limit);
9678 var off_in: i64 = undefined;9679 var off_in: i64 = undefined;
9679 const off_in_ptr: ?*i64 = switch (file_reader.mode) {9680 const off_in_ptr: ?*i64 = switch (file_reader.mode) {
9680 .positional_simple, .streaming_simple => return error.Unimplemented,9681 .positional_simple, .streaming_simple => return error.Unimplemented,
9681 .positional => p: {9682 .positional => p: {
9683 len = @min(len, std.math.maxInt(usize) - file_reader.pos);
9682 off_in = @intCast(file_reader.pos);9684 off_in = @intCast(file_reader.pos);
9683 break :p &off_in;9685 break :p &off_in;
9684 },9686 },
...@@ -9689,7 +9691,7 @@ fn fileWriteFileStreaming(...@@ -9689,7 +9691,7 @@ fn fileWriteFileStreaming(
9689 .linux => n: {9691 .linux => n: {
9690 const syscall: Syscall = try .start();9692 const syscall: Syscall = try .start();
9691 while (true) {9693 while (true) {
9692 const rc = linux_copy_file_range_sys.copy_file_range(in_fd, off_in_ptr, out_fd, null, @intFromEnum(limit), 0);9694 const rc = linux_copy_file_range_sys.copy_file_range(in_fd, off_in_ptr, out_fd, null, len, 0);
9693 switch (linux_copy_file_range_sys.errno(rc)) {9695 switch (linux_copy_file_range_sys.errno(rc)) {
9694 .SUCCESS => {9696 .SUCCESS => {
9695 syscall.finish();9697 syscall.finish();
...@@ -9849,10 +9851,12 @@ fn fileWriteFilePositional(...@@ -9849,10 +9851,12 @@ fn fileWriteFilePositional(
9849 file_reader.interface.toss(n -| header.len);9851 file_reader.interface.toss(n -| header.len);
9850 return n;9852 return n;
9851 }9853 }
9854 var len: usize = @min(@intFromEnum(limit), std.math.maxInt(usize) - offset);
9852 var off_in: i64 = undefined;9855 var off_in: i64 = undefined;
9853 const off_in_ptr: ?*i64 = switch (file_reader.mode) {9856 const off_in_ptr: ?*i64 = switch (file_reader.mode) {
9854 .positional_simple, .streaming_simple => return error.Unimplemented,9857 .positional_simple, .streaming_simple => return error.Unimplemented,
9855 .positional => p: {9858 .positional => p: {
9859 len = @min(len, std.math.maxInt(usize) - file_reader.pos);
9856 off_in = @intCast(file_reader.pos);9860 off_in = @intCast(file_reader.pos);
9857 break :p &off_in;9861 break :p &off_in;
9858 },9862 },
...@@ -9864,7 +9868,7 @@ fn fileWriteFilePositional(...@@ -9864,7 +9868,7 @@ fn fileWriteFilePositional(
9864 .linux => n: {9868 .linux => n: {
9865 const syscall: Syscall = try .start();9869 const syscall: Syscall = try .start();
9866 while (true) {9870 while (true) {
9867 const rc = linux_copy_file_range_sys.copy_file_range(in_fd, off_in_ptr, out_fd, &off_out, @intFromEnum(limit), 0);9871 const rc = linux_copy_file_range_sys.copy_file_range(in_fd, off_in_ptr, out_fd, &off_out, len, 0);
9868 switch (linux_copy_file_range_sys.errno(rc)) {9872 switch (linux_copy_file_range_sys.errno(rc)) {
9869 .SUCCESS => {9873 .SUCCESS => {
9870 syscall.finish();9874 syscall.finish();
...@@ -9888,7 +9892,7 @@ fn fileWriteFilePositional(...@@ -9888,7 +9892,7 @@ fn fileWriteFilePositional(
9888 .IO => return error.InputOutput,9892 .IO => return error.InputOutput,
9889 .NOMEM => return error.SystemResources,9893 .NOMEM => return error.SystemResources,
9890 .NOSPC => return error.NoSpaceLeft,9894 .NOSPC => return error.NoSpaceLeft,
9891 .OVERFLOW => return error.Unseekable,9895 .OVERFLOW => |err| errnoBug(err), // We avoid passing too large a count.
9892 .NXIO => return error.Unseekable,9896 .NXIO => return error.Unseekable,
9893 .SPIPE => return error.Unseekable,9897 .SPIPE => return error.Unseekable,
9894 .PERM => return error.PermissionDenied,9898 .PERM => return error.PermissionDenied,