Skip to content
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

Add a version of io::BufRead::read_until() with a read limit #1427

Closed
BlacklightShining opened this issue Dec 27, 2015 · 4 comments
Closed
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@BlacklightShining
Copy link

So there's an ::std::io::BufRead::read_until(), which is great, except in cases where the input is untrusted (e.g. the context of a server process that listens for connections from the general public): while there's no possibility of an actual buffer overflow, an attacker could trivially cause undesirably high memory usage. Thus, read_until() would have to be avoided, but implementations might end up with what amounts to the same thing, with a limit on the amount of data read.

Given that this is a common usecase (it seems so to me, anyway), I propose adding this variation to BufRead as a provided method:

/// As `read_until()`, except that `buf` will be overwritten, not appended to,
/// and with the additional restriction that not more than `buf.len()` bytes
/// will be read.
fn read_until_full(&mut self, byte: u8, buf: &mut [u8]) -> Result<usize> { ... }

(I'm not particularly attached to the name read_until_full. There's probably a better one for it.)

@BlacklightShining BlacklightShining changed the title Add an io::BufRead::read_until_n() Add a version of io::BufRead::read_until() with a read limit Dec 27, 2015
@troplin
Copy link

troplin commented Apr 30, 2016

You can use

bufreader.take(max_bytes).read_until(byte, &mut buf)

@BlacklightShining
Copy link
Author

…and if you need to reuse bufreader later, you can just do bufreader.by_ref().take(...). Sounds good to me; thanks!

@Centril Centril added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label Feb 23, 2018
@i3Cheese
Copy link

i3Cheese commented Dec 2, 2023

But with take(max_bytes) data after the byte will be lost

@shepmaster
Copy link
Member

That’s what #1427 (comment) is about.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

5 participants