Skip to content

Commit 621790e

Browse files
authored
io: fix take when using evil reader (#4428)
1 parent 7aad428 commit 621790e

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

tokio/src/io/util/take.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,14 @@ impl<R: AsyncRead> AsyncRead for Take<R> {
8484
return Poll::Ready(Ok(()));
8585
}
8686

87+
let buf_ptr = buf.filled().as_ptr();
88+
8789
let me = self.project();
8890
let mut b = buf.take(*me.limit_ as usize);
91+
8992
ready!(me.inner.poll_read(cx, &mut b))?;
93+
assert_eq!(b.filled().as_ptr(), buf_ptr);
94+
9095
let n = b.filled().len();
9196

9297
// We need to update the original ReadBuf

tokio/tests/io_take.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#![warn(rust_2018_idioms)]
22
#![cfg(feature = "full")]
33

4-
use tokio::io::AsyncReadExt;
4+
use std::pin::Pin;
5+
use std::task::{Context, Poll};
6+
use tokio::io::{self, AsyncRead, AsyncReadExt, ReadBuf};
57
use tokio_test::assert_ok;
68

79
#[tokio::test]
@@ -14,3 +16,29 @@ async fn take() {
1416
assert_eq!(n, 4);
1517
assert_eq!(&buf, &b"hell\0\0"[..]);
1618
}
19+
20+
struct BadReader;
21+
22+
impl AsyncRead for BadReader {
23+
fn poll_read(
24+
self: Pin<&mut Self>,
25+
_cx: &mut Context<'_>,
26+
read_buf: &mut ReadBuf<'_>,
27+
) -> Poll<io::Result<()>> {
28+
let vec = vec![0; 10];
29+
30+
let mut buf = ReadBuf::new(vec.leak());
31+
buf.put_slice(&[123; 10]);
32+
*read_buf = buf;
33+
34+
Poll::Ready(Ok(()))
35+
}
36+
}
37+
38+
#[tokio::test]
39+
#[should_panic]
40+
async fn bad_reader_fails() {
41+
let mut buf = Vec::with_capacity(10);
42+
43+
BadReader.take(10).read_buf(&mut buf).await.unwrap();
44+
}

0 commit comments

Comments
 (0)