io: fix unsoundness in read_to_end (#3428)

This commit is contained in:
Alice Ryhl 2021-01-15 00:09:07 +01:00 committed by GitHub
parent 2330edc875
commit ad8fb41103
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 5 deletions

View File

@ -1,8 +1,8 @@
on:
push:
branches: ["master"]
branches: ["master", "tokio-*.x"]
pull_request:
branches: ["master"]
branches: ["master", "tokio-*.x"]
name: CI

View File

@ -72,14 +72,13 @@ fn poll_read_to_end<R: AsyncRead + ?Sized>(
let mut unused_capacity = ReadBuf::uninit(get_unused_capacity(buf));
let ptr = unused_capacity.filled().as_ptr();
ready!(read.poll_read(cx, &mut unused_capacity))?;
assert_eq!(ptr, unused_capacity.filled().as_ptr());
let n = unused_capacity.filled().len();
let new_len = buf.len() + n;
// This should no longer even be possible in safe Rust. An implementor
// would need to have unsafely *replaced* the buffer inside `ReadBuf`,
// which... yolo?
assert!(new_len <= buf.capacity());
unsafe {
buf.set_len(new_len);