Skip to content

Commit

Permalink
Avoid panicking branch in append_to_string
Browse files Browse the repository at this point in the history
  • Loading branch information
a1phyr committed Apr 12, 2024
1 parent 23211b6 commit 2e3ee23
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,10 @@ where
{
let mut g = Guard { len: buf.len(), buf: buf.as_mut_vec() };
let ret = f(g.buf);
if str::from_utf8(&g.buf[g.len..]).is_err() {

// SAFETY: the caller promises to only append data to `buf`
let appended = g.buf.get_unchecked(g.len..);
if str::from_utf8(appended).is_err() {
ret.and_then(|_| Err(Error::INVALID_UTF8))
} else {
g.len = g.buf.len();
Expand Down

0 comments on commit 2e3ee23

Please sign in to comment.