Skip to content

Commit

Permalink
cap more field sizes and relax fuzzer
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Mar 27, 2024
1 parent 42a0933 commit bff28c0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
28 changes: 14 additions & 14 deletions fuzz/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use arbitrary::{Arbitrary, Unstructured};
use std::cmp::min;

#[derive(Clone, Default, Debug, Eq, PartialEq, Arbitrary)]
pub struct Model {
Expand Down Expand Up @@ -76,21 +77,21 @@ impl Arbitrary<'_> for Identifier {
}
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, Arbitrary)]
struct Mode(u32);

impl Arbitrary<'_> for Mode {
fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result<Self> {
u32::arbitrary(u).map(|v| Mode(v & 0o7777))
impl PartialEq for Mode {
fn eq(&self, other: &Mode) -> bool {
self.0 & 0o7777_7777 == other.0 & 0o7777_7777
}
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, Arbitrary)]
struct Timestamp(u64);

impl Arbitrary<'_> for Timestamp {
fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result<Self> {
u.int_in_range(0..=999_999_999_999).map(Timestamp) // TODO: Deal with entire range
impl PartialEq for Timestamp {
fn eq(&self, other: &Timestamp) -> bool {
min(self.0, 999_999_999_999) == min(other.0, 999_999_999_999)
}
}

Expand All @@ -99,11 +100,10 @@ struct Uid(u32);

impl PartialEq for Uid {
fn eq(&self, other: &Uid) -> bool {
truncated_ascii(self.0.to_string(), 6) == truncated_ascii(other.0.to_string(), 6)
let mut left = self.0.to_string();
left.truncate(6);
let mut right = other.0.to_string();
right.truncate(6);
left == right
}
}

fn truncated_ascii(mut ascii: String, width: usize) -> String {
ascii.truncate(width);
ascii
}
9 changes: 5 additions & 4 deletions src/header.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp::min;
use std::collections::HashMap;
use std::fs::Metadata;
use std::io::{self, Error, ErrorKind, Read, Result, Write};
Expand Down Expand Up @@ -244,10 +245,10 @@ impl Header {
writer,
"#1/{:<13}{:<12}{:<6.6}{:<6.6}{:<8o}{:<10}`",
padded_length,
self.mtime,
min(self.mtime, 999_999_999_999),
self.uid.to_string(),
self.gid.to_string(),
self.mode,
self.mode & 0o7777_7777,
self.size + padded_length as u64
)?;
writer.write_all(&self.identifier)?;
Expand All @@ -258,10 +259,10 @@ impl Header {
writeln!(
writer,
"{:<12}{:<6.6}{:<6.6}{:<8o}{:<10}`",
self.mtime,
min(self.mtime, 999_999_999_999),
self.uid.to_string(),
self.gid.to_string(),
self.mode,
self.mode & 0o7777_7777,
self.size
)?;
}
Expand Down

0 comments on commit bff28c0

Please sign in to comment.