Skip to content

Commit 59b6b11

Browse files
sfacklerMark-Simulacrum
authored andcommitted
Fix handling of malicious Readers in read_to_end
1 parent 33b84bb commit 59b6b11

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

library/std/src/io/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,14 @@ where
391391
ret = Ok(g.len - start_len);
392392
break;
393393
}
394-
Ok(n) => g.len += n,
394+
Ok(n) => {
395+
// We can't let g.len overflow which would result in the vec shrinking when the function returns. In
396+
// particular, that could break read_to_string if the shortened buffer doesn't end on a UTF-8 boundary.
397+
// The minimal check would just be a checked_add, but this assert is a bit more precise and should be
398+
// just about the same cost.
399+
assert!(n <= g.buf.len() - g.len);
400+
g.len += n;
401+
}
395402
Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
396403
Err(e) => {
397404
ret = Err(e);

0 commit comments

Comments
 (0)