From e6c07b0628938b0003ecbae0f60b588eebf474aa Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Mon, 11 Jan 2021 17:16:44 -0500 Subject: [PATCH] clarify docs a bit --- library/std/src/io/mod.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index af570ac6e30..f73116ba106 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -388,10 +388,9 @@ fn read_to_end_with_reservation( match r.read(buf) { Ok(0) => return Ok(g.len - start_len), Ok(n) => { - // We can't let g.len overflow which would result in the vec shrinking when the function returns. In - // particular, that could break read_to_string if the shortened buffer doesn't end on a UTF-8 boundary. - // The minimal check would just be a checked_add, but this assert is a bit more precise and should be - // just about the same cost. + // We can't allow bogus values from read. If it is too large, the returned vec could have its length + // set past its capacity, or if it overflows the vec could be shortened which could create an invalid + // string if this is called via read_to_string. assert!(n <= buf.len()); g.len += n; }