Skip to content

Commit 7917c13

Browse files
committed
Merge branch 'gix-config'
2 parents d8ffee2 + 64a4a75 commit 7917c13

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

gix-config/fuzz/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ cargo-fuzz = true
1212
[dependencies]
1313
libfuzzer-sys = "0.4.7"
1414
arbitrary = { version = "1", features = ["derive"] }
15+
bstr = "1.8.0"
1516

1617
[dependencies.gix-config]
1718
path = ".."
@@ -25,3 +26,9 @@ name = "parse"
2526
path = "fuzz_targets/parse.rs"
2627
test = false
2728
doc = false
29+
30+
[[bin]]
31+
name = "fuzz_file"
32+
path = "fuzz_targets/fuzz_file.rs"
33+
test = false
34+
doc = false
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
for section in file.sections() {
37+
for key in section.keys() {
38+
_ = black_box(
39+
section
40+
.value_implicit(key.as_ref())
41+
.expect("The key exists, so should the value."),
42+
);
43+
}
44+
}
45+
46+
let roundtrip_as_string: Vec<u8> = file.to_bstring().into();
47+
_ = unwrap_or_return!(black_box(File::from_bytes_no_includes(
48+
&roundtrip_as_string,
49+
meta.clone(),
50+
options.clone(),
51+
)));
52+
});

0 commit comments

Comments
 (0)