Skip to content

Commit

Permalink
Merge branch 'fuzz-gix-config-value'
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jan 1, 2024
2 parents 0878344 + 85476a2 commit 03ec4e9
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 1 deletion.
4 changes: 4 additions & 0 deletions gix-config-value/fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
corpus
artifacts
coverage
28 changes: 28 additions & 0 deletions gix-config-value/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "gix-config-value-fuzz"
version = "0.0.0"
publish = false
edition = "2021"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"
gix-config-value = { path = ".." }
anyhow = "1.0.78"
bstr = "1.9.0"
arbitrary = { version = "1.3.2", features = ["derive"] }

# Prevent this from interfering with workspaces
[workspace]
members = ["."]

[profile.release]
debug = 1

[[bin]]
name = "fuzz_value"
path = "fuzz_targets/fuzz_value.rs"
test = false
doc = false
36 changes: 36 additions & 0 deletions gix-config-value/fuzz/fuzz_targets/fuzz_value.dict
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Names
"normal"
"default"
"black"
"brightblack"
"red"
"brightred"
"green"
"brightgreen"
"yellow"
"brightyellow"
"blue"
"brightblue"
"magenta"
"brightmagenta"
"cyan"
"brightcyan"
"white"
"brightwhite"

# Attributes
"reset"
"bold"
"nobold"
"dim"
"nodim"
"ul"
"noul"
"blink"
"noblink"
"reverse"
"noreverse"
"italic"
"noitalic"
"strike"
"nostrike"
53 changes: 53 additions & 0 deletions gix-config-value/fuzz/fuzz_targets/fuzz_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#![no_main]

extern crate libfuzzer_sys;

use anyhow::Result;
use arbitrary::Arbitrary;
use bstr::BStr;
use gix_config_value::{
color::{Attribute, Name},
path::interpolate::Context,
Boolean, Color, Integer, Path,
};
use libfuzzer_sys::fuzz_target;
use std::{borrow::Cow, fmt::Write, hint::black_box, str::FromStr};

#[derive(Debug, Arbitrary)]
struct Ctx<'a> {
bool_str: &'a [u8],
color_str: &'a [u8],
integer_str: &'a [u8],
path_str: &'a [u8],
attribute_str: &'a str,
name_str: &'a str,
}

fn fuzz(ctx: Ctx) -> Result<()> {
let b = Boolean::try_from(BStr::new(ctx.bool_str))?;
_ = black_box(b.is_true());

_ = black_box(Color::try_from(BStr::new(ctx.color_str)))?;

let mut buf = String::with_capacity(128);
let a = Attribute::from_str(ctx.attribute_str)?;
_ = black_box(write!(&mut buf, "{a}"));

let name = Name::from_str(ctx.name_str)?;
_ = black_box(write!(&mut buf, "{name}"));

let i = Integer::try_from(BStr::new(ctx.integer_str))?;
_ = black_box(i.to_decimal());

let p = Path::from(Cow::Borrowed(BStr::new(ctx.path_str)));
_ = black_box(p.interpolate(Context::default()));

Ok(())
}

fuzz_target!(|ctx: Ctx| {
if let Err(e) = fuzz(ctx) {
// Excersize display/debug fmt code.
_ = black_box(format!("{e} {e:?}"));
}
});
2 changes: 1 addition & 1 deletion gix-config-value/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl FromStr for Name {
}

if let Some(s) = s.strip_prefix('#') {
if s.len() == 6 {
if s.len() == 6 && s.is_char_boundary(2) && s.is_char_boundary(4) && s.is_char_boundary(6) {
let rgb = (
u8::from_str_radix(&s[..2], 16),
u8::from_str_radix(&s[2..4], 16),
Expand Down
1 change: 1 addition & 0 deletions gix-config-value/tests/value/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ mod name {
assert!(Name::from_str("#").is_err());
assert!(Name::from_str("#fff").is_err());
assert!(Name::from_str("#gggggg").is_err());
assert!(Name::from_str("#=»©=").is_err());
}
}

Expand Down

0 comments on commit 03ec4e9

Please sign in to comment.