Repository navigation
Fix Reader.Limited hangs #25109
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
base: master
Are you sure you want to change the base?
Fix Reader.Limited hangs #25109
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ pub fn init(reader: *Reader, limit: Limit, buffer: []u8) Limited { | |
|
|
||
| fn stream(r: *Reader, w: *Writer, limit: Limit) Reader.StreamError!usize { | ||
| const l: *Limited = @fieldParentPtr("interface", r); | ||
| if (l.remaining == .nothing) return error.EndOfStream; | ||
This comment was marked as resolved.
Sorry, something went wrong.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now reading 0 bytes is a no-op, not an error. It's like subtracting 0 from a number. I don't think that should change.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, my bad, I confused the interface implementation function here (in |
||
| const combined_limit = limit.min(l.remaining); | ||
| const n = try l.unlimited.stream(w, combined_limit); | ||
| l.remaining = l.remaining.subtract(n).?; | ||
|
|
@@ -51,8 +52,51 @@ test stream { | |
|
|
||
| fn discard(r: *Reader, limit: Limit) Reader.Error!usize { | ||
| const l: *Limited = @fieldParentPtr("interface", r); | ||
| if (l.remaining == .nothing) return error.EndOfStream; | ||
| const combined_limit = limit.min(l.remaining); | ||
| const n = try l.unlimited.discard(combined_limit); | ||
| l.remaining = l.remaining.subtract(n).?; | ||
| return n; | ||
| } | ||
|
|
||
| test "end of stream, read, hit limit exactly" { | ||
| var f: Reader = .fixed("i'm dying"); | ||
| var l = f.limited(.limited(4), &.{}); | ||
| const r = &l.interface; | ||
|
|
||
| var buf: [2]u8 = undefined; | ||
| try r.readSliceAll(&buf); | ||
| try r.readSliceAll(&buf); | ||
| try std.testing.expectError(error.EndOfStream, l.interface.readSliceAll(&buf)); | ||
| } | ||
|
|
||
| test "end of stream, read, hit limit after partial read" { | ||
| var f: Reader = .fixed("i'm dying"); | ||
| var l = f.limited(.limited(5), &.{}); | ||
| const r = &l.interface; | ||
|
|
||
| var buf: [2]u8 = undefined; | ||
| try r.readSliceAll(&buf); | ||
| try r.readSliceAll(&buf); | ||
| try std.testing.expectError(error.EndOfStream, l.interface.readSliceAll(&buf)); | ||
| } | ||
|
|
||
| test "end of stream, discard, hit limit exactly" { | ||
| var f: Reader = .fixed("i'm dying"); | ||
| var l = f.limited(.limited(4), &.{}); | ||
| const r = &l.interface; | ||
|
|
||
| try r.discardAll(2); | ||
| try r.discardAll(2); | ||
| try std.testing.expectError(error.EndOfStream, l.interface.discardAll(2)); | ||
| } | ||
|
|
||
| test "end of stream, discard, hit limit after partial read" { | ||
| var f: Reader = .fixed("i'm dying"); | ||
| var l = f.limited(.limited(5), &.{}); | ||
| const r = &l.interface; | ||
|
|
||
| try r.discardAll(2); | ||
| try r.discardAll(2); | ||
| try std.testing.expectError(error.EndOfStream, l.interface.discardAll(2)); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be logic inside the interface. To enforce this rule, it must be an assertion. There's already an assertion below for this on line 183 but it will be skipped if an error is returned. I suggest instead of this to do the assertion after calling
streambut before returning an error. Also, this rule must be mentioned in the VTable documentation forstream.