-
-
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 064afef
Showing
3 changed files
with
67 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,39 @@ | ||
cfg_io_blocking! { | ||
use std::{ | ||
io::{self, Read}, | ||
mem::MaybeUninit, | ||
}; | ||
|
||
use crate::io::ReadBuf; | ||
use super::blocking::Buf; | ||
|
||
const PAYLOAD: &[u8] = b"hello test!"; | ||
|
||
// 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(); 64]; | ||
buf.ensure_capacity_for(&ReadBuf::uninit(&mut dst), MAX_BUF_SIZE); | ||
miri_assert_init(buf.bytes()); | ||
let res = buf.read_from(&mut EnsureInitReader(PAYLOAD)).unwrap(); | ||
assert_eq!(res, PAYLOAD.len()); | ||
assert_eq!(buf.bytes(), PAYLOAD); | ||
} | ||
|
||
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 {} | ||
} | ||
} |