History

Merge branch 'fuzz-gix-config-value'

Sebastian Thiel committed 2 years ago

Commit 03ec4e92 parents 0878344 85476a26 files · +123 −1Browse files

Unified · Split

6 files
  1. gix-config-value/fuzz/.gitignore +4 −0
  2. gix-config-value/fuzz/Cargo.toml +28 −0
  3. gix-config-value/fuzz/fuzz_targets/fuzz_value.dict +36 −0
  4. gix-config-value/fuzz/fuzz_targets/fuzz_value.rs +53 −0
  5. gix-config-value/src/color.rs +1 −1
  6. gix-config-value/tests/value/color.rs +1 −0
gix-config-value/fuzz/.gitignore+4−0
@@ -0,0 +1,4 @@
1target
2corpus
3artifacts
4coverage
gix-config-value/fuzz/Cargo.toml+28−0
@@ -0,0 +1,28 @@
1[package]
2name = "gix-config-value-fuzz"
3version = "0.0.0"
4publish = false
5edition = "2021"
6
7[package.metadata]
8cargo-fuzz = true
9
10[dependencies]
11libfuzzer-sys = "0.4"
12gix-config-value = { path = ".." }
13anyhow = "1.0.78"
14bstr = "1.9.0"
15arbitrary = { version = "1.3.2", features = ["derive"] }
16
17# Prevent this from interfering with workspaces
18[workspace]
19members = ["."]
20
21[profile.release]
22debug = 1
23
24[[bin]]
25name = "fuzz_value"
26path = "fuzz_targets/fuzz_value.rs"
27test = false
28doc = false
gix-config-value/fuzz/fuzz_targets/fuzz_value.dict+36−0
@@ -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"
gix-config-value/fuzz/fuzz_targets/fuzz_value.rs+53−0
@@ -0,0 +1,53 @@
1#![no_main]
2
3extern crate libfuzzer_sys;
4
5use anyhow::Result;
6use arbitrary::Arbitrary;
7use bstr::BStr;
8use gix_config_value::{
9 color::{Attribute, Name},
10 path::interpolate::Context,
11 Boolean, Color, Integer, Path,
12};
13use libfuzzer_sys::fuzz_target;
14use std::{borrow::Cow, fmt::Write, hint::black_box, str::FromStr};
15
16#[derive(Debug, Arbitrary)]
17struct 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
26fn 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 mut buf = String::with_capacity(128);
33 let a = Attribute::from_str(ctx.attribute_str)?;
34 _ = black_box(write!(&mut buf, "{a}"));
35
36 let name = Name::from_str(ctx.name_str)?;
37 _ = black_box(write!(&mut buf, "{name}"));
38
39 let i = Integer::try_from(BStr::new(ctx.integer_str))?;
40 _ = black_box(i.to_decimal());
41
42 let p = Path::from(Cow::Borrowed(BStr::new(ctx.path_str)));
43 _ = black_box(p.interpolate(Context::default()));
44
45 Ok(())
46}
47
48fuzz_target!(|ctx: Ctx| {
49 if let Err(e) = fuzz(ctx) {
50 // Excersize display/debug fmt code.
51 _ = black_box(format!("{e} {e:?}"));
52 }
53});
gix-config-value/src/color.rs+1−1
@@ -204,7 +204,7 @@ impl FromStr for Name { · 203 unmodified lines
204 }204 }
205 205
206 if let Some(s) = s.strip_prefix('#') {206 if let Some(s) = s.strip_prefix('#') {
207 if s.len() == 6 {207 if s.len() == 6 && s.is_char_boundary(2) && s.is_char_boundary(4) && s.is_char_boundary(6) {
208 let rgb = (208 let rgb = (
209 u8::from_str_radix(&s[..2], 16),209 u8::from_str_radix(&s[..2], 16),
210 u8::from_str_radix(&s[2..4], 16),210 u8::from_str_radix(&s[2..4], 16),
gix-config-value/tests/value/color.rs+1−0
@@ -54,6 +54,7 @@ mod name { · 53 unmodified lines
54 assert!(Name::from_str("#").is_err());54 assert!(Name::from_str("#").is_err());
55 assert!(Name::from_str("#fff").is_err());55 assert!(Name::from_str("#fff").is_err());
56 assert!(Name::from_str("#gggggg").is_err());56 assert!(Name::from_str("#gggggg").is_err());
57 assert!(Name::from_str("#=»©=").is_err());
57 }58 }
58}59}
59 60