Skip to content

Add limit option to Net::BufferedIO#readuntil - #67

Merged
hsbt merged 2 commits into
masterfrom
readuntil-limit
Aug 25, 2026
Merged

Add limit option to Net::BufferedIO#readuntil#67
hsbt merged 2 commits into
masterfrom
readuntil-limit

Conversation

@hsbt

@hsbt hsbt commented Aug 25, 2026

Copy link
Copy Markdown
Member

Net::BufferedIO#readuntil buffers indefinitely until the terminator arrives, so a peer that never sends it can grow the read buffer without bound. This adds a limit: keyword that raises the new Net::ReadLimitExceeded when the terminator is not found within the given number of bytes. Protocol implementations can use it to cap line length.

io = Net::BufferedIO.new(socket)
io.readuntil("\n", limit: 16384)

The limit is the largest result readuntil may return, counting the terminator, and it never truncates the way the limit of IO#gets does. The exception derives from Net::ProtocolError rather than IOError, because an over-long line is the peer violating the protocol and not an I/O failure, and because IOError would put it in the generic socket-error rescue that Net::HTTP retries on.

Reported at ruby/net-http#315. net-http will call this from Net::HTTPResponse.read_line guarded by defined?(Net::ReadLimitExceeded), so it depends on a net-protocol release shipping this first.

hsbt and others added 2 commits August 25, 2026 20:14
readuntil buffers until the terminator arrives, so a peer that never
sends one grows the read buffer without bound. The limit lets a protocol
implementation cap what a single read may return, raising the new
Net::ReadLimitExceeded instead of reading on.

The exception derives from ProtocolError because an over-long line is
the peer violating the protocol rather than an I/O failure, and because
IOError would place it in the generic socket-error rescue that Net::HTTP
retries on.

ruby/net-http#315

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The floor's two reasons fit in one block, and the two tests that
restated them are named for what they cover.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@hsbt
hsbt force-pushed the readuntil-limit branch from 6d13b93 to 6ffd258 Compare August 25, 2026 11:15
@hsbt
hsbt merged commit 74c2348 into master Aug 25, 2026
72 checks passed
@hsbt
hsbt deleted the readuntil-limit branch August 25, 2026 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant