Skip to content

Commit 1446002

Browse files
gix-config: Add a fuzz harness for File
1 parent ebc051c commit 1446002

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

gix-config/fuzz/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,9 @@ name = "fuzz_file_section"
3232
path = "fuzz_targets/fuzz_file_section.rs"
3333
test = false
3434
doc = false
35+
36+
[[bin]]
37+
name = "fuzz_file"
38+
path = "fuzz_targets/fuzz_file.rs"
39+
test = false
40+
doc = false
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#![no_main]
2+
3+
use arbitrary::Arbitrary;
4+
use gix_config::{
5+
file::{init::Options, Metadata},
6+
File,
7+
};
8+
use libfuzzer_sys::fuzz_target;
9+
use std::hint::black_box;
10+
11+
#[derive(Arbitrary, Debug)]
12+
struct Ctx<'a> {
13+
input: &'a [u8],
14+
sections_by_name: &'a str,
15+
}
16+
17+
macro_rules! unwrap_or_return {
18+
($e:expr) => {
19+
match $e {
20+
Ok(val) => val,
21+
Err(_) => return,
22+
}
23+
};
24+
}
25+
26+
fuzz_target!(|ctx: Ctx| {
27+
let meta = Metadata::default();
28+
let options = Options::default();
29+
let file = unwrap_or_return!(File::from_bytes_no_includes(&ctx.input, meta.clone(), options.clone()));
30+
_ = black_box(file.sections().count());
31+
_ = black_box(file.sections_and_ids().count());
32+
_ = black_box(file.sections_and_postmatter().count());
33+
_ = black_box(file.sections_by_name(ctx.sections_by_name).map(|x| x.count()));
34+
_ = black_box(file.frontmatter());
35+
36+
let roundtrip_as_string: Vec<u8> = file.to_bstring().into();
37+
_ = unwrap_or_return!(black_box(File::from_bytes_no_includes(
38+
&roundtrip_as_string,
39+
meta.clone(),
40+
options.clone(),
41+
)));
42+
});

0 commit comments

Comments
 (0)