From 1a983536f3ee798b18224658d5d4b406c952c64d Mon Sep 17 00:00:00 2001 From: Kornel Date: Sun, 1 Jan 2023 17:49:05 +0000 Subject: [PATCH 1/2] Document read_line gotcha --- library/std/src/io/mod.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 23a13523fc275..3f940279bf2d1 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 @@ -2154,6 +2156,7 @@ pub trait BufRead: Read { /// or EOF. /// /// [`Ok(0)`]: Ok + /// [`clear`]: String:::clear /// /// # Errors /// From 3a6ceeb18f7f2bf6f5c8c647d4f747fdf47bef7b Mon Sep 17 00:00:00 2001 From: Kornel Date: Sun, 1 Jan 2023 18:04:26 +0000 Subject: [PATCH 2/2] Document a way to limit read_line length --- library/std/src/io/mod.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 3f940279bf2d1..de528e85368cb 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -2153,10 +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 + /// [`clear`]: String::clear + /// [`take`]: crate::io::Read::take /// /// # Errors ///