1
1
#![ no_main]
2
2
3
+ use anyhow:: Result ;
3
4
use arbitrary:: Arbitrary ;
5
+ use bstr:: BStr ;
4
6
use gix_config:: {
5
7
file:: { init:: Options , Metadata } ,
6
8
File ,
7
9
} ;
8
10
use libfuzzer_sys:: fuzz_target;
11
+ use std:: error:: Error ;
12
+ use std:: fmt;
9
13
use std:: hint:: black_box;
10
14
11
15
#[ derive( Arbitrary , Debug ) ]
12
16
struct Ctx < ' a > {
13
17
input : & ' a [ u8 ] ,
14
18
sections_by_name : & ' a str ,
19
+ section_subsection_key_triples : Vec < ( & ' a str , Option < & ' a [ u8 ] > , & ' a str ) > ,
15
20
}
16
21
17
- macro_rules! unwrap_or_return {
18
- ( $e: expr) => {
19
- match $e {
20
- Ok ( val) => val,
21
- Err ( _) => return ,
22
- }
23
- } ;
24
- }
22
+ const DEFAULT_TRIPLE : ( & str , Option < & ' static [ u8 ] > , & str ) = ( "section" , Some ( b"subsection" ) , "key" ) ;
25
23
26
- fuzz_target ! ( | ctx: Ctx | {
24
+ fn fuzz ( ctx : Ctx ) -> Result < ( ) > {
27
25
let meta = Metadata :: default ( ) ;
28
26
let options = Options :: default ( ) ;
29
- let file = unwrap_or_return!( File :: from_bytes_no_includes( & ctx. input, meta. clone( ) , options. clone( ) ) ) ;
27
+ let file = File :: from_bytes_no_includes ( & ctx. input , meta. clone ( ) , options. clone ( ) ) ?;
28
+
29
+ let mut triples = ctx. section_subsection_key_triples . iter ( ) ;
30
+
31
+ let ( section_name, subsection_name, key) = triples. next ( ) . unwrap_or ( & DEFAULT_TRIPLE ) ;
32
+ _ = black_box ( file. string ( section_name, subsection_name. map ( |x| BStr :: new ( x) ) , key) ) ;
33
+ _ = black_box ( file. string_by_key ( BStr :: new ( key) ) ) ;
34
+ _ = black_box ( file. string_filter ( section_name, subsection_name. map ( |x| BStr :: new ( x) ) , key, & mut |_| false ) ) ;
35
+ _ = black_box ( file. string_filter_by_key ( BStr :: new ( key) , & mut |_| false ) ) ;
36
+
37
+ let ( section_name, subsection_name, key) = triples. next ( ) . unwrap_or ( & DEFAULT_TRIPLE ) ;
38
+ _ = black_box ( file. path ( section_name, subsection_name. map ( |x| BStr :: new ( x) ) , key) ) ;
39
+ _ = black_box ( file. path_by_key ( BStr :: new ( key) ) ) ;
40
+ _ = black_box ( file. path_filter ( section_name, subsection_name. map ( |x| BStr :: new ( x) ) , key, & mut |_| false ) ) ;
41
+ _ = black_box ( file. path_filter_by_key ( BStr :: new ( key) , & mut |_| false ) ) ;
42
+
43
+ let ( section_name, subsection_name, key) = triples. next ( ) . unwrap_or ( & DEFAULT_TRIPLE ) ;
44
+ _ = black_box ( file. boolean ( section_name, subsection_name. map ( |x| BStr :: new ( x) ) , key) ) ;
45
+ _ = black_box ( file. boolean_by_key ( BStr :: new ( key) ) ) ;
46
+ _ = black_box ( file. boolean_filter ( section_name, subsection_name. map ( |x| BStr :: new ( x) ) , key, & mut |_| false ) ) ;
47
+ _ = black_box ( file. boolean_filter_by_key ( BStr :: new ( key) , & mut |_| false ) ) ;
48
+
49
+ let ( section_name, subsection_name, key) = triples. next ( ) . unwrap_or ( & DEFAULT_TRIPLE ) ;
50
+ _ = black_box ( file. integer ( section_name, subsection_name. map ( |x| BStr :: new ( x) ) , key) ) ;
51
+ _ = black_box ( file. integer_by_key ( BStr :: new ( key) ) ) ;
52
+ _ = black_box ( file. integer_filter ( section_name, subsection_name. map ( |x| BStr :: new ( x) ) , key, & mut |_| false ) ) ;
53
+ _ = black_box ( file. integer_filter_by_key ( BStr :: new ( key) , & mut |_| false ) ) ;
54
+
55
+ let ( section_name, subsection_name, key) = triples. next ( ) . unwrap_or ( & DEFAULT_TRIPLE ) ;
56
+ _ = black_box ( file. strings ( section_name, subsection_name. map ( |x| BStr :: new ( x) ) , key) ) ;
57
+ _ = black_box ( file. strings_by_key ( BStr :: new ( key) ) ) ;
58
+ _ = black_box ( file. strings_filter ( section_name, subsection_name. map ( |x| BStr :: new ( x) ) , key, & mut |_| false ) ) ;
59
+ _ = black_box ( file. strings_filter_by_key ( BStr :: new ( key) , & mut |_| false ) ) ;
60
+
61
+ let ( section_name, subsection_name, key) = triples. next ( ) . unwrap_or ( & DEFAULT_TRIPLE ) ;
62
+ _ = black_box ( file. integers ( section_name, subsection_name. map ( |x| BStr :: new ( x) ) , key) ) ;
63
+ _ = black_box ( file. integers_by_key ( BStr :: new ( key) ) ) ;
64
+ _ = black_box ( file. integers_filter ( section_name, subsection_name. map ( |x| BStr :: new ( x) ) , key, & mut |_| false ) ) ;
65
+ _ = black_box ( file. integers_filter_by_key ( BStr :: new ( key) , & mut |_| false ) ) ;
66
+
67
+ let ( section_name, subsection_name, key) = triples. next ( ) . unwrap_or ( & DEFAULT_TRIPLE ) ;
68
+ _ = black_box ( file. integers ( section_name, subsection_name. map ( |x| BStr :: new ( x) ) , key) ) ;
69
+ _ = black_box ( file. integers_by_key ( BStr :: new ( key) ) ) ;
70
+ _ = black_box ( file. integers_filter ( section_name, subsection_name. map ( |x| BStr :: new ( x) ) , key, & mut |_| false ) ) ;
71
+ _ = black_box ( file. integers_filter_by_key ( BStr :: new ( key) , & mut |_| false ) ) ;
72
+
30
73
_ = black_box ( file. sections ( ) . count ( ) ) ;
31
74
_ = black_box ( file. sections_and_ids ( ) . count ( ) ) ;
32
75
_ = black_box ( file. sections_and_postmatter ( ) . count ( ) ) ;
@@ -44,9 +87,14 @@ fuzz_target!(|ctx: Ctx| {
44
87
}
45
88
46
89
let roundtrip_as_string: Vec < u8 > = file. to_bstring ( ) . into ( ) ;
47
- _ = unwrap_or_return! ( black_box( File :: from_bytes_no_includes(
90
+ _ = black_box ( File :: from_bytes_no_includes (
48
91
& roundtrip_as_string,
49
92
meta. clone ( ) ,
50
93
options. clone ( ) ,
51
- ) ) ) ;
94
+ ) ) ?;
95
+ Ok ( ( ) )
96
+ }
97
+
98
+ fuzz_target ! ( |ctx: Ctx | {
99
+ _ = black_box( fuzz( ctx) ) ;
52
100
} ) ;
0 commit comments