Skip to content

Commit

Permalink
Remove needess checks in CString::arbitrary()
Browse files Browse the repository at this point in the history
Use CString::from_vec_unchecked() to get a new CString, as all zero
bytes have been removed, improving performance. In debug builds, this
function will still check if any zero bytes are present.
  • Loading branch information
00xc committed Nov 4, 2023
1 parent 273d7b6 commit e5f3d2a
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,8 @@ impl<'a> Arbitrary<'a> for CString {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
<Vec<u8> as Arbitrary>::arbitrary(u).map(|mut x| {
x.retain(|&c| c != 0);
Self::new(x).unwrap()
// SAFETY: all zero bytes have been removed
unsafe { Self::from_vec_unchecked(x) }
})
}

Expand Down

0 comments on commit e5f3d2a

Please sign in to comment.