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

fix(Unstructured)!: don't produce meaningless data if exhausted #108

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1166,15 +1166,17 @@ mod test {

#[test]
fn finite_buffer_fill_buffer() {
let x = [1, 2, 3, 4];
let x = [1, 2, 3, 4, 5];
let mut rb = Unstructured::new(&x);
let mut z = [0; 2];
rb.fill_buffer(&mut z).unwrap();
assert_eq!(z, [1, 2]);
rb.fill_buffer(&mut z).unwrap();
assert_eq!(z, [3, 4]);
rb.fill_buffer(&mut z).unwrap();
assert_eq!(z, [0, 0]);
assert_eq!(z, [5, 0]);

assert!(rb.fill_buffer(&mut z).is_err());
}

#[test]
Expand Down
16 changes: 10 additions & 6 deletions src/unstructured.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,28 +468,32 @@ impl<'a> Unstructured<'a> {
/// `Arbitrary` implementations like `<Vec<u8>>::arbitrary` and
/// `String::arbitrary` over using this method directly.
///
/// If this `Unstructured` does not have enough underlying data to fill the
/// whole `buffer`, it pads the buffer out with zeros.
/// If this `Unstructured` only has enough underlying data to fill only part of
/// the whole `buffer`, it pads the rest of the buffer with zeros. If the
/// `Unstructured` is completely exhausted, an error is returned.
///
/// # Example
///
/// ```
/// use arbitrary::Unstructured;
///
/// let mut u = Unstructured::new(&[1, 2, 3, 4]);
/// let mut u = Unstructured::new(&[1, 2, 3]);
///
/// let mut buf = [0; 2];
///
/// assert!(u.fill_buffer(&mut buf).is_ok());
/// assert_eq!(buf, [1, 2]);
///
/// assert!(u.fill_buffer(&mut buf).is_ok());
/// assert_eq!(buf, [3, 4]);
/// assert_eq!(buf, [3, 0]);
///
/// assert!(u.fill_buffer(&mut buf).is_ok());
/// assert_eq!(buf, [0, 0]);
/// assert!(u.fill_buffer(&mut buf).is_err());
/// ```
pub fn fill_buffer(&mut self, buffer: &mut [u8]) -> Result<()> {
if self.is_empty() {
return Err(Error::NotEnoughData);
}

let n = std::cmp::min(buffer.len(), self.data.len());
buffer[..n].copy_from_slice(&self.data[..n]);
for byte in buffer[n..].iter_mut() {
Expand Down
11 changes: 11 additions & 0 deletions tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ fn recursive() {
);
}

#[test]
fn recursive_first_variant() {
#[derive(PartialEq, Eq, Debug, Arbitrary)]
enum Nat {
Succ(Box<Nat>),
Zero,
}

assert!(Nat::arbitrary(&mut Unstructured::new(&[])).is_err());
}

#[derive(Arbitrary, Debug)]
struct Generic<T> {
inner: T,
Expand Down