Skip to content

Commit 8695b9f

Browse files
daurnimatorandrewrk
authored andcommitted
std: use reader.skipBytes to avoid infinite loop in writeFileAllUnseekable
skipBytes correctly handles EOF for us
1 parent 0ab8ae9 commit 8695b9f

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

lib/std/fs/file.zig

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ pub const File = struct {
698698
header_count: usize = 0,
699699
};
700700

701-
pub const WriteFileError = ReadError || WriteError;
701+
pub const WriteFileError = ReadError || error{EndOfStream} || WriteError;
702702

703703
pub fn writeFileAll(self: File, in_file: File, args: WriteFileOptions) WriteFileError!void {
704704
return self.writeFileAllSendfile(in_file, args) catch |err| switch (err) {
@@ -722,16 +722,9 @@ pub const File = struct {
722722

723723
try self.writevAll(headers);
724724

725+
try in_file.reader().skipBytes(args.in_offset, .{ .buf_size = 4096 });
726+
725727
var buffer: [4096]u8 = undefined;
726-
{
727-
var index: usize = 0;
728-
// Skip in_offset bytes.
729-
while (index < args.in_offset) {
730-
const ask = math.min(buffer.len, args.in_offset - index);
731-
const amt = try in_file.read(buffer[0..ask]);
732-
index += amt;
733-
}
734-
}
735728
const in_len = args.in_len orelse math.maxInt(u64);
736729
var index: usize = 0;
737730
while (index < in_len) {

0 commit comments

Comments
 (0)