Skip to content

Commit ebc051c

Browse files
gix-config: Add fuzzer for file::Section
1 parent d8ffee2 commit ebc051c

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

Cargo.lock

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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_section"
32+
path = "fuzz_targets/fuzz_file_section.rs"
33+
test = false
34+
doc = false
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#![no_main]
2+
3+
use arbitrary::Arbitrary;
4+
use bstr::BStr;
5+
use gix_config::file::{Metadata, Section};
6+
use libfuzzer_sys::fuzz_target;
7+
use std::borrow::Cow;
8+
use std::hint::black_box;
9+
10+
#[derive(Arbitrary, Debug)]
11+
struct Ctx<'a> {
12+
name: Cow<'a, str>,
13+
subsection: Option<&'a [u8]>,
14+
#[arbitrary(default)]
15+
meta: Metadata,
16+
value_key: &'a str,
17+
}
18+
19+
macro_rules! unwrap_or_return {
20+
($e:expr) => {
21+
match $e {
22+
Ok(val) => val,
23+
Err(_) => return,
24+
}
25+
};
26+
}
27+
28+
fuzz_target!(|ctx: Ctx| {
29+
let section = unwrap_or_return!(Section::new(
30+
ctx.name.clone(),
31+
ctx.subsection.map(|x| Cow::from(BStr::new(x))),
32+
ctx.meta.clone(),
33+
));
34+
_ = black_box(section.values(ctx.value_key));
35+
for key in section.keys() {
36+
_ = black_box(section.value(key).expect("The key exists, so should the value."));
37+
}
38+
_ = black_box(section.to_bstring());
39+
});

0 commit comments

Comments
 (0)