Skip to content

Commit 3f4db4a

Browse files
Add gix-config-value fuzzer
- Add fuzzer - Add associated dict
1 parent 0878344 commit 3f4db4a

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

gix-config-value/fuzz/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
target
2+
corpus
3+
artifacts
4+
coverage

gix-config-value/fuzz/Cargo.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "gix-config-value-fuzz"
3+
version = "0.0.0"
4+
publish = false
5+
edition = "2021"
6+
7+
[package.metadata]
8+
cargo-fuzz = true
9+
10+
[dependencies]
11+
libfuzzer-sys = "0.4"
12+
gix-config-value = { path = ".." }
13+
anyhow = "1.0.78"
14+
bstr = "1.9.0"
15+
arbitrary = { version = "1.3.2", features = ["derive"] }
16+
17+
# Prevent this from interfering with workspaces
18+
[workspace]
19+
members = ["."]
20+
21+
[profile.release]
22+
debug = 1
23+
24+
[[bin]]
25+
name = "fuzz_value"
26+
path = "fuzz_targets/fuzz_value.rs"
27+
test = false
28+
doc = false
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Names
2+
"normal"
3+
"default"
4+
"black"
5+
"brightblack"
6+
"red"
7+
"brightred"
8+
"green"
9+
"brightgreen"
10+
"yellow"
11+
"brightyellow"
12+
"blue"
13+
"brightblue"
14+
"magenta"
15+
"brightmagenta"
16+
"cyan"
17+
"brightcyan"
18+
"white"
19+
"brightwhite"
20+
21+
# Attributes
22+
"reset"
23+
"bold"
24+
"nobold"
25+
"dim"
26+
"nodim"
27+
"ul"
28+
"noul"
29+
"blink"
30+
"noblink"
31+
"reverse"
32+
"noreverse"
33+
"italic"
34+
"noitalic"
35+
"strike"
36+
"nostrike"
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#![no_main]
2+
3+
extern crate libfuzzer_sys;
4+
5+
use anyhow::Result;
6+
use arbitrary::Arbitrary;
7+
use bstr::{BStr, BString};
8+
use gix_config_value::{
9+
color::{Attribute, Name},
10+
path::interpolate::Context,
11+
Boolean, Color, Integer, Path,
12+
};
13+
use libfuzzer_sys::fuzz_target;
14+
use std::{borrow::Cow, hint::black_box, str::FromStr};
15+
16+
#[derive(Debug, Arbitrary)]
17+
struct Ctx<'a> {
18+
bool_str: &'a [u8],
19+
color_str: &'a [u8],
20+
integer_str: &'a [u8],
21+
path_str: &'a [u8],
22+
attribute_str: &'a str,
23+
name_str: &'a str,
24+
}
25+
26+
fn fuzz(ctx: Ctx) -> Result<()> {
27+
let b = Boolean::try_from(BStr::new(ctx.bool_str))?;
28+
_ = black_box(b.is_true());
29+
30+
_ = black_box(Color::try_from(BStr::new(ctx.color_str)))?;
31+
32+
let a = Attribute::from_str(ctx.attribute_str)?;
33+
_ = black_box(format!("{a}"));
34+
35+
let name = Name::from_str(ctx.name_str)?;
36+
_ = black_box(format!("{name}"));
37+
38+
let i = Integer::try_from(BStr::new(ctx.integer_str))?;
39+
_ = black_box(i.to_decimal());
40+
41+
let p = Path::try_from(Cow::Owned(BString::from(ctx.path_str)))?;
42+
_ = black_box(p.interpolate(Context::default()));
43+
44+
Ok(())
45+
}
46+
47+
fuzz_target!(|ctx: Ctx| {
48+
if let Err(e) = fuzz(ctx) {
49+
// Excersize display/debug fmt code.
50+
_ = black_box(format!("{e} {e:?}"));
51+
}
52+
});

0 commit comments

Comments
 (0)