Skip to content

Commit ebe402d

Browse files
committed
Fix handling of malicious Readers in read_to_end
1 parent c97f11a commit ebe402d

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

library/std/src/io/mod.rs

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

0 commit comments

Comments
 (0)