11#![ no_main]
22
3- use std:: cell:: RefCell ;
4- use libfuzzer_sys:: fuzz_target;
53use arbitrary:: Arbitrary ;
4+ use libfuzzer_sys:: fuzz_target;
5+ use std:: cell:: RefCell ;
66use std:: io:: { Cursor , Read , Seek , Write } ;
7- use std:: path:: { PathBuf } ;
7+ use std:: path:: PathBuf ;
88
9- #[ derive( Arbitrary , Clone , Debug ) ]
9+ #[ derive( Arbitrary , Clone , Debug ) ]
1010pub enum BasicFileOperation {
1111 WriteNormalFile {
1212 contents : Vec < Vec < u8 > > ,
13- options : zip_next:: write:: FileOptions ,
13+ options : zip_next:: write:: FullFileOptions ,
1414 } ,
15- WriteDirectory ( zip_next:: write:: FileOptions ) ,
15+ WriteDirectory ( zip_next:: write:: FullFileOptions ) ,
1616 WriteSymlinkWithTarget {
1717 target : Box < PathBuf > ,
18- options : zip_next:: write:: FileOptions ,
18+ options : zip_next:: write:: FullFileOptions ,
1919 } ,
2020 ShallowCopy ( Box < FileOperation > ) ,
2121 DeepCopy ( Box < FileOperation > ) ,
2222}
2323
24- #[ derive( Arbitrary , Clone , Debug ) ]
24+ #[ derive( Arbitrary , Clone , Debug ) ]
2525pub struct FileOperation {
2626 basic : BasicFileOperation ,
2727 name : String ,
2828 reopen : bool ,
2929 // 'abort' flag is separate, to prevent trying to copy an aborted file
3030}
3131
32- #[ derive( Arbitrary , Clone , Debug ) ]
32+ #[ derive( Arbitrary , Clone , Debug ) ]
3333pub struct FuzzTestCase {
3434 comment : Vec < u8 > ,
3535 operations : Vec < ( FileOperation , bool ) > ,
@@ -47,14 +47,25 @@ impl FileOperation {
4747 }
4848}
4949
50- fn do_operation < T > ( writer : & mut RefCell < zip_next:: ZipWriter < T > > ,
51- operation : FileOperation ,
52- abort : bool , flush_on_finish_file : bool ) -> Result < ( ) , Box < dyn std:: error:: Error > >
53- where T : Read + Write + Seek {
54- writer. borrow_mut ( ) . set_flush_on_finish_file ( flush_on_finish_file) ;
50+ fn do_operation < T > (
51+ writer : & mut RefCell < zip_next:: ZipWriter < T > > ,
52+ operation : FileOperation ,
53+ abort : bool ,
54+ flush_on_finish_file : bool ,
55+ ) -> Result < ( ) , Box < dyn std:: error:: Error > >
56+ where
57+ T : Read + Write + Seek ,
58+ {
59+ writer
60+ . borrow_mut ( )
61+ . set_flush_on_finish_file ( flush_on_finish_file) ;
5562 let name = operation. name ;
5663 match operation. basic {
57- BasicFileOperation :: WriteNormalFile { contents, mut options, ..} => {
64+ BasicFileOperation :: WriteNormalFile {
65+ contents,
66+ mut options,
67+ ..
68+ } => {
5869 let uncompressed_size = contents. iter ( ) . map ( Vec :: len) . sum :: < usize > ( ) ;
5970 if uncompressed_size >= u32:: MAX as usize {
6071 options = options. large_file ( true ) ;
@@ -67,8 +78,10 @@ fn do_operation<T>(writer: &mut RefCell<zip_next::ZipWriter<T>>,
6778 BasicFileOperation :: WriteDirectory ( options) => {
6879 writer. borrow_mut ( ) . add_directory ( name, options) ?;
6980 }
70- BasicFileOperation :: WriteSymlinkWithTarget { target, options} => {
71- writer. borrow_mut ( ) . add_symlink ( name, target. to_string_lossy ( ) , options) ?;
81+ BasicFileOperation :: WriteSymlinkWithTarget { target, options } => {
82+ writer
83+ . borrow_mut ( )
84+ . add_symlink ( name, target. to_string_lossy ( ) , options) ?;
7285 }
7386 BasicFileOperation :: ShallowCopy ( base) => {
7487 let base_name = base. referenceable_name ( ) ;
@@ -86,8 +99,8 @@ fn do_operation<T>(writer: &mut RefCell<zip_next::ZipWriter<T>>,
8699 }
87100 if operation. reopen {
88101 let old_comment = writer. borrow ( ) . get_raw_comment ( ) . to_owned ( ) ;
89- let new_writer = zip_next :: ZipWriter :: new_append (
90- writer. borrow_mut ( ) . finish ( ) . unwrap ( ) ) . unwrap ( ) ;
102+ let new_writer =
103+ zip_next :: ZipWriter :: new_append ( writer. borrow_mut ( ) . finish ( ) . unwrap ( ) ) . unwrap ( ) ;
91104 assert_eq ! ( & old_comment, new_writer. get_raw_comment( ) ) ;
92105 * writer = new_writer. into ( ) ;
93106 }
@@ -98,7 +111,12 @@ fuzz_target!(|test_case: FuzzTestCase| {
98111 let mut writer = RefCell :: new( zip_next:: ZipWriter :: new( Cursor :: new( Vec :: new( ) ) ) ) ;
99112 writer. borrow_mut( ) . set_raw_comment( test_case. comment) ;
100113 for ( operation, abort) in test_case. operations {
101- let _ = do_operation( & mut writer, operation, abort, test_case. flush_on_finish_file) ;
114+ let _ = do_operation(
115+ & mut writer,
116+ operation,
117+ abort,
118+ test_case. flush_on_finish_file,
119+ ) ;
102120 }
103121 let _ = zip_next:: ZipArchive :: new( writer. borrow_mut( ) . finish( ) . unwrap( ) ) ;
104- } ) ;
122+ } ) ;
0 commit comments