Skip to content

Commit ee11bfd

Browse files
committed
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.
2 parents e11cb36 + 3a6ceeb commit ee11bfd

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

library/std/src/io/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -2137,8 +2137,10 @@ pub trait BufRead: Read {
21372137
}
21382138

21392139
/// Read all bytes until a newline (the `0xA` byte) is reached, and append
2140-
/// them to the provided buffer. You do not need to clear the buffer before
2141-
/// appending.
2140+
/// them to the provided `String` buffer.
2141+
///
2142+
/// Previous content of the buffer will be preserved. To avoid appending to
2143+
/// the buffer, you need to [`clear`] it first.
21422144
///
21432145
/// This function will read bytes from the underlying stream until the
21442146
/// newline delimiter (the `0xA` byte) or EOF is found. Once found, all bytes
@@ -2151,9 +2153,11 @@ pub trait BufRead: Read {
21512153
///
21522154
/// This function is blocking and should be used carefully: it is possible for
21532155
/// an attacker to continuously send bytes without ever sending a newline
2154-
/// or EOF.
2156+
/// or EOF. You can use [`take`] to limit the maximum number of bytes read.
21552157
///
21562158
/// [`Ok(0)`]: Ok
2159+
/// [`clear`]: String::clear
2160+
/// [`take`]: crate::io::Read::take
21572161
///
21582162
/// # Errors
21592163
///

0 commit comments

Comments
 (0)