1 use futures::{
2 io::{self, AsyncRead, AsyncReadExt},
3 task::{Context, Poll},
4 };
5 use std::pin::Pin;
6
7 #[test]
8 #[should_panic(expected = "assertion failed: n <= buf.len()")]
issue2310()9 fn issue2310() {
10 struct MyRead {
11 first: bool,
12 }
13
14 impl MyRead {
15 pub fn new() -> Self {
16 MyRead { first: false }
17 }
18 }
19
20 impl AsyncRead for MyRead {
21 fn poll_read(
22 mut self: Pin<&mut Self>,
23 _cx: &mut Context,
24 _buf: &mut [u8],
25 ) -> Poll<io::Result<usize>> {
26 Poll::Ready(if !self.first {
27 self.first = true;
28 // First iteration: return more than the buffer size
29 Ok(64)
30 } else {
31 // Second iteration: indicate that we are done
32 Ok(0)
33 })
34 }
35 }
36
37 struct VecWrapper {
38 inner: Vec<u8>,
39 }
40
41 impl VecWrapper {
42 pub fn new() -> Self {
43 VecWrapper { inner: Vec::new() }
44 }
45 }
46
47 impl Drop for VecWrapper {
48 fn drop(&mut self) {
49 // Observe uninitialized bytes
50 println!("{:?}", &self.inner);
51 // Overwrite heap contents
52 for b in &mut self.inner {
53 *b = 0x90;
54 }
55 }
56 }
57
58 futures::executor::block_on(async {
59 let mut vec = VecWrapper::new();
60 let mut read = MyRead::new();
61
62 read.read_to_end(&mut vec.inner).await.unwrap();
63 })
64 }
65