Skip to content

Commit

Permalink
Merge branch 'config-fuzz'
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jul 12, 2024
2 parents af6446a + 0dc2a0c commit ed66357
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions gix-config/src/file/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,19 @@ pub(crate) fn ends_with_newline(e: &[crate::parse::Event<'_>], nl: impl AsRef<[u
}

pub(crate) fn extract_newline<'a>(e: &'a Event<'_>) -> Option<&'a BStr> {
match e {
Event::Newline(b) => b.as_ref().into(),
_ => None,
}
Some(match e {
Event::Newline(b) => {
let nl = b.as_ref();

// Newlines are parsed consecutively, be sure we only take the smallest possible variant
if nl.contains(&b'\r') {
"\r\n".into()
} else {
"\n".into()
}
}
_ => return None,
})
}

pub(crate) fn platform_newline() -> &'static BStr {
Expand Down
2 changes: 1 addition & 1 deletion gix-config/src/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub enum Event<'a> {
Value(Cow<'a, BStr>),
/// Represents any token used to signify a newline character. On Unix
/// platforms, this is typically just `\n`, but can be any valid newline
/// sequence. Multiple newlines (such as `\n\n`) will be merged as a single
/// *sequence*. Multiple newlines (such as `\n\n`) will be merged as a single
/// newline event containing a string of multiple newline characters.
Newline(Cow<'a, BStr>),
/// Any value that isn't completed. This occurs when the value is continued
Expand Down
24 changes: 23 additions & 1 deletion gix-config/tests/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::borrow::Cow;

use bstr::{BStr, ByteSlice};
use gix_config::File;
use gix_testtools::fixture_path_standalone;

pub fn cow_str(s: &str) -> Cow<'_, BStr> {
Cow::Borrowed(s.as_bytes().as_bstr())
Expand All @@ -27,7 +28,7 @@ mod open {
}

#[test]
fn fuzzed() {
fn fuzzed_stackoverflow() {
let file = File::from_bytes_no_includes(
include_bytes!("../fixtures/fuzzed/stackoverflow-01.config"),
gix_config::file::Metadata::default(),
Expand All @@ -43,6 +44,27 @@ fn fuzzed() {
}
}

#[test]
fn fuzzed_long_runtime() -> crate::Result {
let config = std::fs::read(fixture_path_standalone("fuzzed/long-parsetime.config"))?;
let file = File::from_bytes_no_includes(&config, gix_config::file::Metadata::default(), Default::default())?;
assert_eq!(file.sections().count(), 52);
assert!(file.to_bstring().len() < 1200000);
File::from_bytes_no_includes(
&file.to_bstring(),
gix_config::file::Metadata::default(),
Default::default(),
)?;

let mut mutated_file = file.clone();
mutated_file.append(file);
assert_eq!(mutated_file.sections().count(), 52 * 2);
let serialized = mutated_file.to_bstring();
assert!(serialized.len() < 2400000);
File::from_bytes_no_includes(&serialized, gix_config::file::Metadata::default(), Default::default())?;
Ok(())
}

mod access;
mod impls;
mod init;
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion gix-config/tests/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn usage() {
peak = ByteSize(ALLOCATOR.max_allocated() as u64),
);
assert!(
used < 60 * 1024 * 1024,
used < 80 * 1024 * 1024,
"we should now start using more memory than anticipated, to keep mem-amplification low"
);
}

0 comments on commit ed66357

Please sign in to comment.