@@ -9,6 +9,7 @@ use gix_config::{
9
9
File ,
10
10
} ;
11
11
use libfuzzer_sys:: fuzz_target;
12
+ use std:: borrow:: Cow ;
12
13
use std:: hint:: black_box;
13
14
use std:: str;
14
15
@@ -56,6 +57,12 @@ fn fuzz_immutable(file: &File, section_name: &str, subsection_name: &Option<BStr
56
57
_ = black_box ( file. integers_by_key ( key) ) ;
57
58
_ = black_box ( file. integers_filter ( section_name, subsection_name, key, & mut |_| false ) ) ;
58
59
_ = 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 ( ) ) ;
59
66
}
60
67
61
68
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
70
77
} ;
71
78
72
79
// Mutate section.
73
- {
80
+ let section_id = {
74
81
let mut section = file. section_mut ( section_name, subsection_name) ?;
75
82
section. push_newline ( ) ;
76
83
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
79
86
if let Some ( ( key, value) ) = kv_pair {
80
87
section. push_with_comment ( key, Some ( & value) , "Popped" ) ;
81
88
}
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) ) ;
82
103
}
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
+ ) ) ;
83
122
84
123
// Singular raw.
85
124
_ = black_box (
@@ -95,7 +134,7 @@ fn fuzz_mutable(file: &mut File, section_name: &str, subsection_name: &Option<BS
95
134
fn fuzz ( input : & [ u8 ] ) -> Result < ( ) > {
96
135
let meta = Metadata :: default ( ) ;
97
136
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 ( ) ) ?;
99
138
100
139
let section_triples: Vec < _ > = file
101
140
. sections ( )
@@ -118,22 +157,22 @@ fn fuzz(input: &[u8]) -> Result<()> {
118
157
black_box ( fuzz_immutable ( & file, & section_name, & subsection_name, & key) ) ;
119
158
}
120
159
160
+ let mut mutated_file = file. clone ( ) ;
161
+
121
162
for section_triple in section_triples. iter ( ) {
122
163
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) ) ;
124
165
}
125
166
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) ) ;
130
168
131
- let roundtrip_as_string: Vec < u8 > = file . to_bstring ( ) . into ( ) ;
169
+ let roundtrip_as_string: Vec < u8 > = mutated_file . to_bstring ( ) . into ( ) ;
132
170
_ = black_box ( File :: from_bytes_no_includes (
133
171
& roundtrip_as_string,
134
172
meta. clone ( ) ,
135
173
options. clone ( ) ,
136
- ) ) ?;
174
+ ) ?) ;
175
+
137
176
Ok ( ( ) )
138
177
}
139
178
0 commit comments