arbitrary/foreign/alloc/ffi/c_str.rs
1use {
2 crate::{Arbitrary, Result, Unstructured},
3 std::ffi::CString,
4};
5
6impl<'a> Arbitrary<'a> for CString {
7 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
8 <Vec<u8> as Arbitrary>::arbitrary(u).map(|mut x| {
9 x.retain(|&c| c != 0);
10 // SAFETY:
11 // Contract from `CString::from_vec_unchecked`: the vector must not contain
12 // any interior nul (zero) bytes.
13 // Evidence: `x.retain(|&c| c != 0)` removes all bytes equal to `0` from the
14 // vector `x`. Consequently, `x` contains no nul bytes, which guarantees
15 // it has no interior nul bytes.
16 unsafe { Self::from_vec_unchecked(x) }
17 })
18 }
19
20 #[inline]
21 fn size_hint(depth: usize) -> (usize, Option<usize>) {
22 <Vec<u8> as Arbitrary>::size_hint(depth)
23 }
24}