-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
io: fix unsound
Buf::ensure_capacity_for
- Loading branch information
1 parent
970d880
commit 5759994
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -293,3 +293,6 @@ cfg_io_blocking! { | |
pub(crate) use crate::blocking::JoinHandle as Blocking; | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use std::{ | ||
io::{self, Read}, | ||
mem::MaybeUninit, | ||
}; | ||
|
||
use super::blocking::Buf; | ||
use crate::io::ReadBuf; | ||
|
||
// Have miri check that `Buf::ensure_capacity_for` initializes its length. | ||
#[test] | ||
fn buf_ensure_capacity_for_len_is_init() { | ||
const MAX_BUF_SIZE: usize = 128; | ||
|
||
let mut buf = Buf::with_capacity(0); | ||
|
||
let mut dst = [MaybeUninit::uninit(); 96]; | ||
buf.ensure_capacity_for(&ReadBuf::uninit(&mut dst), MAX_BUF_SIZE); | ||
miri_assert_init(buf.bytes()); | ||
let res = buf | ||
.read_from(&mut EnsureInitReader(&[123u8; 64][..])) | ||
.unwrap(); | ||
assert_eq!(res, 64); | ||
assert_eq!(buf.bytes(), [123u8; 64]); | ||
} | ||
|
||
struct EnsureInitReader<R>(R); | ||
|
||
impl<R: Read> Read for EnsureInitReader<R> { | ||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | ||
miri_assert_init(buf); | ||
self.0.read(buf) | ||
} | ||
} | ||
|
||
fn miri_assert_init(buf: &[u8]) { | ||
for &_b in buf {} | ||
} |