Skip to content

Commit b9f8b6e

Browse files
whatisaphoneandrewrk
authored andcommitted
Fix Reader.Limited end of stream conditions
1 parent e5a55f6 commit b9f8b6e

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

lib/std/Io/Reader/Limited.zig

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub fn init(reader: *Reader, limit: Limit, buffer: []u8) Limited {
2727

2828
fn stream(r: *Reader, w: *Writer, limit: Limit) Reader.StreamError!usize {
2929
const l: *Limited = @fieldParentPtr("interface", r);
30+
if (l.remaining == .nothing) return error.EndOfStream;
3031
const combined_limit = limit.min(l.remaining);
3132
const n = try l.unlimited.stream(w, combined_limit);
3233
l.remaining = l.remaining.subtract(n).?;
@@ -51,8 +52,51 @@ test stream {
5152

5253
fn discard(r: *Reader, limit: Limit) Reader.Error!usize {
5354
const l: *Limited = @fieldParentPtr("interface", r);
55+
if (l.remaining == .nothing) return error.EndOfStream;
5456
const combined_limit = limit.min(l.remaining);
5557
const n = try l.unlimited.discard(combined_limit);
5658
l.remaining = l.remaining.subtract(n).?;
5759
return n;
5860
}
61+
62+
test "end of stream, read, hit limit exactly" {
63+
var f: Reader = .fixed("i'm dying");
64+
var l = f.limited(.limited(4), &.{});
65+
const r = &l.interface;
66+
67+
var buf: [2]u8 = undefined;
68+
try r.readSliceAll(&buf);
69+
try r.readSliceAll(&buf);
70+
try std.testing.expectError(error.EndOfStream, l.interface.readSliceAll(&buf));
71+
}
72+
73+
test "end of stream, read, hit limit after partial read" {
74+
var f: Reader = .fixed("i'm dying");
75+
var l = f.limited(.limited(5), &.{});
76+
const r = &l.interface;
77+
78+
var buf: [2]u8 = undefined;
79+
try r.readSliceAll(&buf);
80+
try r.readSliceAll(&buf);
81+
try std.testing.expectError(error.EndOfStream, l.interface.readSliceAll(&buf));
82+
}
83+
84+
test "end of stream, discard, hit limit exactly" {
85+
var f: Reader = .fixed("i'm dying");
86+
var l = f.limited(.limited(4), &.{});
87+
const r = &l.interface;
88+
89+
try r.discardAll(2);
90+
try r.discardAll(2);
91+
try std.testing.expectError(error.EndOfStream, l.interface.discardAll(2));
92+
}
93+
94+
test "end of stream, discard, hit limit after partial read" {
95+
var f: Reader = .fixed("i'm dying");
96+
var l = f.limited(.limited(5), &.{});
97+
const r = &l.interface;
98+
99+
try r.discardAll(2);
100+
try r.discardAll(2);
101+
try std.testing.expectError(error.EndOfStream, l.interface.discardAll(2));
102+
}

0 commit comments

Comments
 (0)