/src/gitoxide/gix-config-value/fuzz/fuzz_targets/fuzz_value.rs
Line | Count | Source |
1 | | #![no_main] |
2 | | |
3 | | extern crate libfuzzer_sys; |
4 | | |
5 | | use anyhow::Result; |
6 | | use arbitrary::Arbitrary; |
7 | | use bstr::BStr; |
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, fmt::Write, 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 | 3.67k | fn fuzz(ctx: Ctx) -> Result<()> { |
27 | 3.67k | let b = Boolean::try_from(BStr::new(ctx.bool_str))?; |
28 | 2.72k | _ = black_box(b.is_true()); |
29 | | |
30 | 2.72k | _ = black_box(Color::try_from(BStr::new(ctx.color_str)))?; |
31 | | |
32 | 1.62k | let mut buf = String::with_capacity(128); |
33 | 1.62k | let a = Attribute::from_str(ctx.attribute_str)?; |
34 | 1.10k | _ = black_box(write!(&mut buf, "{a}")); |
35 | | |
36 | 1.10k | let name = Name::from_str(ctx.name_str)?; |
37 | 1.07k | _ = black_box(write!(&mut buf, "{name}")); |
38 | | |
39 | 1.07k | let i = Integer::try_from(BStr::new(ctx.integer_str))?; |
40 | 687 | _ = black_box(i.to_decimal()); |
41 | | |
42 | 687 | let p = Path::from(Cow::Borrowed(BStr::new(ctx.path_str))); |
43 | 687 | _ = black_box(p.interpolate(Context::default())); |
44 | | |
45 | 687 | Ok(()) |
46 | 3.67k | } |
47 | | |
48 | | fuzz_target!(|ctx: Ctx| { |
49 | | if let Err(e) = fuzz(ctx) { |
50 | | // Exercise display/debug fmt code. |
51 | | _ = black_box(format!("{e} {e:?}")); |
52 | | } |
53 | | }); |