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