Skip to content

Commit 34e4a16

Browse files
committed
Merge branch 'fuzz-gix-config'
2 parents c3983c6 + 2420547 commit 34e4a16

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# A random set lookup/config pairs.
2+
"[core]"
3+
"core"
4+
5+
"[http]"
6+
"http"
7+
8+
"[section \"subsection\"]"
9+
"section"
10+
"subsection"
11+
12+
# Directives
13+
"include"
14+
"includeIf"
15+
"gitdir:"
16+
"onbranch:"
17+
"hasconfig:"
18+
"remote"
19+
"path"

gix-config/fuzz/fuzz_targets/fuzz_file.rs

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use gix_config::{
99
File,
1010
};
1111
use libfuzzer_sys::fuzz_target;
12+
use std::borrow::Cow;
1213
use std::hint::black_box;
1314
use std::str;
1415

@@ -56,6 +57,12 @@ fn fuzz_immutable(file: &File, section_name: &str, subsection_name: &Option<BStr
5657
_ = black_box(file.integers_by_key(key));
5758
_ = black_box(file.integers_filter(section_name, subsection_name, key, &mut |_| false));
5859
_ = black_box(file.integers_filter_by_key(key, &mut |_| false));
60+
61+
// Sections and frontmatter.
62+
_ = black_box(file.sections_and_ids().count());
63+
_ = black_box(file.sections_and_postmatter().count());
64+
_ = black_box(file.sections_by_name("section").map(|x| x.count()));
65+
_ = black_box(file.frontmatter());
5966
}
6067

6168
fn fuzz_mutable(file: &mut File, section_name: &str, subsection_name: &Option<BString>, key: &str) -> Result<()> {
@@ -70,7 +77,7 @@ fn fuzz_mutable(file: &mut File, section_name: &str, subsection_name: &Option<BS
7077
};
7178

7279
// Mutate section.
73-
{
80+
let section_id = {
7481
let mut section = file.section_mut(section_name, subsection_name)?;
7582
section.push_newline();
7683
section.set(key.to_string().try_into()?, BStr::new("Set value"));
@@ -79,7 +86,39 @@ fn fuzz_mutable(file: &mut File, section_name: &str, subsection_name: &Option<BS
7986
if let Some((key, value)) = kv_pair {
8087
section.push_with_comment(key, Some(&value), "Popped");
8188
}
89+
section.id()
90+
};
91+
92+
_ = black_box(file.section_mut_by_key(key));
93+
_ = black_box(file.section_mut_by_id(section_id));
94+
95+
let new_section_name = section_name.to_string() + "_new";
96+
_ = black_box(file.section_mut_or_create_new(&new_section_name, subsection_name));
97+
_ = black_box(file.section_mut_or_create_new_filter(&new_section_name, subsection_name, &mut |_| false));
98+
99+
_ = black_box(file.section_mut_filter(section_name, subsection_name, &mut |_| false));
100+
_ = black_box(file.section_mut_filter_by_key(key, &mut |_| false));
101+
if let Some(removed_section) = file.remove_section(&new_section_name, subsection_name) {
102+
_ = black_box(file.push_section(removed_section));
82103
}
104+
_ = black_box(file.new_section(Cow::Owned(new_section_name.clone()), None));
105+
let renamed_section_name = section_name.to_string() + "_renamed";
106+
let renamed_subsection_name: Option<Cow<'_, BStr>> =
107+
subsection_name.map(|x| Cow::Owned((x.to_string() + "_renamed").into()));
108+
_ = black_box(file.rename_section(
109+
&new_section_name.clone(),
110+
subsection_name,
111+
Cow::Owned(renamed_section_name.clone()),
112+
renamed_subsection_name.clone(),
113+
));
114+
115+
_ = black_box(file.rename_section_filter(
116+
&new_section_name.clone(),
117+
subsection_name,
118+
Cow::Owned(renamed_section_name.clone()),
119+
renamed_subsection_name.clone(),
120+
&mut |_| false,
121+
));
83122

84123
// Singular raw.
85124
_ = black_box(
@@ -95,7 +134,7 @@ fn fuzz_mutable(file: &mut File, section_name: &str, subsection_name: &Option<BS
95134
fn fuzz(input: &[u8]) -> Result<()> {
96135
let meta = Metadata::default();
97136
let options = Options::default();
98-
let mut file = File::from_bytes_no_includes(input, meta.clone(), options.clone())?;
137+
let file = File::from_bytes_no_includes(input, meta.clone(), options.clone())?;
99138

100139
let section_triples: Vec<_> = file
101140
.sections()
@@ -118,22 +157,22 @@ fn fuzz(input: &[u8]) -> Result<()> {
118157
black_box(fuzz_immutable(&file, &section_name, &subsection_name, &key));
119158
}
120159

160+
let mut mutated_file = file.clone();
161+
121162
for section_triple in section_triples.iter() {
122163
let (section_name, subsection_name, key) = section_triple;
123-
_ = black_box(fuzz_mutable(&mut file, &section_name, &subsection_name, &key));
164+
_ = black_box(fuzz_mutable(&mut mutated_file, &section_name, &subsection_name, &key));
124165
}
125166

126-
_ = black_box(file.sections_and_ids().count());
127-
_ = black_box(file.sections_and_postmatter().count());
128-
_ = black_box(file.sections_by_name("section").map(|x| x.count()));
129-
_ = black_box(file.frontmatter());
167+
_ = black_box(mutated_file.append(file));
130168

131-
let roundtrip_as_string: Vec<u8> = file.to_bstring().into();
169+
let roundtrip_as_string: Vec<u8> = mutated_file.to_bstring().into();
132170
_ = black_box(File::from_bytes_no_includes(
133171
&roundtrip_as_string,
134172
meta.clone(),
135173
options.clone(),
136-
))?;
174+
)?);
175+
137176
Ok(())
138177
}
139178

0 commit comments

Comments
 (0)