Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Arbitrary for &[u8]. #67

Merged
merged 1 commit into from Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -32,7 +32,7 @@ Released YYYY-MM-DD.

### Added

*
* The `Arbitrary` trait is now implemented for `&[u8]`. [#67](https://github.com/rust-fuzz/arbitrary/pull/67)

### Changed

Expand All @@ -42,6 +42,8 @@ Released YYYY-MM-DD.

*

--------------------------------------------------------------------------------

## 1.0.0-rc1

Released 2020-11-25.
Expand Down
34 changes: 34 additions & 0 deletions src/lib.rs
Expand Up @@ -626,6 +626,22 @@ arbitrary_array! { 32, (T, a) (T, b) (T, c) (T, d) (T, e) (T, f) (T, g) (T, h)
(T, z) (T, aa) (T, ab) (T, ac) (T, ad) (T, ae) (T, af)
(T, ag) }

impl<'a> Arbitrary<'a> for &'a [u8] {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
let len = u.arbitrary_len::<u8>()?;
u.bytes(len)
}

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
Ok(u.take_rest())
}

#[inline]
fn size_hint(depth: usize) -> (usize, Option<usize>) {
<usize as Arbitrary>::size_hint(depth)
}
}

impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Vec<A> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
u.arbitrary_iter()?.collect()
Expand Down Expand Up @@ -1020,6 +1036,24 @@ mod test {
assert_eq!(expected, actual);
}

#[test]
fn arbitrary_for_bytes() {
let x = [1, 2, 3, 4, 4];
let mut buf = Unstructured::new(&x);
let expected = &[1, 2, 3, 4];
let actual = <&[u8] as Arbitrary>::arbitrary(&mut buf).unwrap();
assert_eq!(expected, actual);
}
Comment on lines +1039 to +1046
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this test still pass with the length prefix stuff? I expect it to pass when using arbitrary_take_rest but not with regular arbitrary, where I instead I would expect that we read a length (which is read from the end of the data) and then get some prefix of [1, 2, 3] depending on how arbitrary_len maps 4 into the 0..3 range.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the latest commit– does that match what's in your head?


#[test]
fn arbitrary_take_rest_for_bytes() {
let x = [1, 2, 3, 4];
let buf = Unstructured::new(&x);
let expected = &[1, 2, 3, 4];
let actual = <&[u8] as Arbitrary>::arbitrary_take_rest(buf).unwrap();
assert_eq!(expected, actual);
}

#[test]
fn arbitrary_collection() {
let x = [
Expand Down