Skip to content

Reader: fix fill when implementation writes to reader directly #24677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions lib/std/Io/Reader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1065,10 +1065,13 @@ fn fillUnbuffered(r: *Reader, n: usize) Error!void {
};
while (r.end < r.seek + n) {
writer.end = r.end;
r.end += r.vtable.stream(r, &writer, .limited(r.buffer.len - r.end)) catch |err| switch (err) {
const streamed = r.vtable.stream(r, &writer, .limited(r.buffer.len - r.end)) catch |err| switch (err) {
error.WriteFailed => unreachable,
error.ReadFailed, error.EndOfStream => |e| return e,
};
// Doing `r.end += r.vtable.stream` is not possible since `r.end` may be modified
// by the implementation, differing from the already evaluated value.
r.end += streamed;
}
}

Expand All @@ -1084,10 +1087,13 @@ pub fn fillMore(r: *Reader) Error!void {
.end = r.end,
.vtable = &.{ .drain = Writer.fixedDrain },
};
r.end += r.vtable.stream(r, &writer, .limited(r.buffer.len - r.end)) catch |err| switch (err) {
const streamed = r.vtable.stream(r, &writer, .limited(r.buffer.len - r.end)) catch |err| switch (err) {
error.WriteFailed => unreachable,
else => |e| return e,
};
// Doing `r.end += r.vtable.stream` is not possible since `r.end` may be modified
// by the implementation, differing from the already evaluated value.
r.end += streamed;
}

/// Returns the next byte from the stream or returns `error.EndOfStream`.
Expand Down Expand Up @@ -1519,6 +1525,26 @@ test fill {
try r.fill(3);
}

test "fill writing directly to reader buffer" {
var buf: [2]u8 = undefined;
var indirect_r: Reader = .{
.buffer = &buf,
.seek = 0,
.end = 0,
.vtable = &.{
.stream = struct {
fn stream(r: *Reader, _: *Writer, _: Limit) StreamError!usize {
@memset(r.buffer[r.seek..], 0xff);
r.end = r.buffer.len;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually this is not a valid use of the API

Copy link
Contributor Author

@gooncreeper gooncreeper Aug 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrewrk Apologies, though what is invalid with it? The vtable doc comment says you can do this and flate.Decompress.streamIndirectInner takes advantage of this.

return 0;
}
}.stream,
},
};
try indirect_r.fill(2);
try std.testing.expectEqualSlices(u8, &.{ 0xff, 0xff }, indirect_r.buffered());
}

test takeByte {
var r: Reader = .fixed("ab");
try testing.expectEqual('a', try r.takeByte());
Expand Down