diff options
| author | bors <bors@rust-lang.org> | 2023-01-02 01:15:25 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-01-02 01:15:25 +0000 |
| commit | ee11bfd8a189462713ea1ede36f4796575ddf894 (patch) | |
| tree | 707547aa9f70f00062068b812335bdf305fc6d32 | |
| parent | e11cb36c751550cf197e23440d53f04156c3a2a3 (diff) | |
| parent | 3a6ceeb18f7f2bf6f5c8c647d4f747fdf47bef7b (diff) | |
| download | rust-ee11bfd8a189462713ea1ede36f4796575ddf894.tar.gz rust-ee11bfd8a189462713ea1ede36f4796575ddf894.zip | |
Auto merge of #106352 - kornelski:read_line-doc, r=scottmcm
Document read_line gotchas 1. The "You do not need to clear the buffer before appending" advice is ambiguous, because it depends what you use this function for. For a rather common case of reading individual lines in a loop, it _is_ necessary to clear the buffer. 2. The docs warn about a DoS risk. I've added a hint how to mitigate unbounded memory growth.
| -rw-r--r-- | library/std/src/io/mod.rs | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 23a13523fc2..de528e85368 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -2137,8 +2137,10 @@ pub trait BufRead: Read { } /// Read all bytes until a newline (the `0xA` byte) is reached, and append - /// them to the provided buffer. You do not need to clear the buffer before - /// appending. + /// them to the provided `String` buffer. + /// + /// Previous content of the buffer will be preserved. To avoid appending to + /// the buffer, you need to [`clear`] it first. /// /// This function will read bytes from the underlying stream until the /// newline delimiter (the `0xA` byte) or EOF is found. Once found, all bytes @@ -2151,9 +2153,11 @@ pub trait BufRead: Read { /// /// This function is blocking and should be used carefully: it is possible for /// an attacker to continuously send bytes without ever sending a newline - /// or EOF. + /// or EOF. You can use [`take`] to limit the maximum number of bytes read. /// /// [`Ok(0)`]: Ok + /// [`clear`]: String::clear + /// [`take`]: crate::io::Read::take /// /// # Errors /// |
