Skip to content

Commit f804284

Browse files
committed
Avoid panicking branch in append_to_string
1 parent 3089f51 commit f804284

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Diff for: std/src/io/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,10 @@ where
384384
{
385385
let mut g = Guard { len: buf.len(), buf: buf.as_mut_vec() };
386386
let ret = f(g.buf);
387-
if str::from_utf8(&g.buf[g.len..]).is_err() {
387+
388+
// SAFETY: the caller promises to only append data to `buf`
389+
let appended = g.buf.get_unchecked(g.len..);
390+
if str::from_utf8(appended).is_err() {
388391
ret.and_then(|_| Err(Error::INVALID_UTF8))
389392
} else {
390393
g.len = g.buf.len();

0 commit comments

Comments
 (0)