From 94df73ea04cc07a66edda92dfdb02510b445426e Mon Sep 17 00:00:00 2001 From: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Date: Fri, 14 Jun 2024 00:17:21 -0700 Subject: [PATCH] chore: Fix boxed_local warning (can borrow instead) --- src/types.rs | 2 +- src/write.rs | 44 ++++++++++++++++++++++++++++++++++---------- tests/end_to_end.rs | 4 +++- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/types.rs b/src/types.rs index f5dbab12..26f94b02 100644 --- a/src/types.rs +++ b/src/types.rs @@ -620,7 +620,7 @@ impl ZipFileData { aes_extra_data_start: u64, compression_method: crate::compression::CompressionMethod, aes_mode: Option<(AesMode, AesVendorVersion, CompressionMethod)>, - extra_field: Box<[u8]>, + extra_field: &[u8], ) -> Self where S: Into>, diff --git a/src/write.rs b/src/write.rs index e0abd5b1..ca09250f 100644 --- a/src/write.rs +++ b/src/write.rs @@ -339,8 +339,8 @@ impl ExtendedFileOptions { let header_id = data.read_u16_le()?; if header_id <= 31 || EXTRA_FIELD_MAPPING - .iter() - .any(|&mapped| mapped == header_id) + .iter() + .any(|&mapped| mapped == header_id) { return Err(ZipError::Io(io::Error::new( io::ErrorKind::Other, @@ -871,9 +871,8 @@ impl ZipWriter { let mut aes_extra_data_start = 0; #[cfg(feature = "aes-crypto")] if let Some(EncryptWith::Aes { mode, .. }) = options.encrypt_with { - let aes_dummy_extra_data = vec![ - 0x02, 0x00, 0x41, 0x45, mode as u8, 0x00, 0x00, - ].into_boxed_slice(); + let aes_dummy_extra_data = + vec![0x02, 0x00, 0x41, 0x45, mode as u8, 0x00, 0x00].into_boxed_slice(); extensions.add_extra_data(0x9901, aes_dummy_extra_data, false)?; } { @@ -896,7 +895,7 @@ impl ZipWriter { aes_extra_data_start, compression_method, aes_mode, - extensions.extra_data.to_vec().into_boxed_slice(), + &extensions.extra_data, ); parse_extra_field(&mut file)?; file.version_made_by = file.version_made_by.max(file.version_needed() as u8); @@ -926,7 +925,6 @@ impl ZipWriter { } let header_end = writer.stream_position()?; - file.extra_data_start = Some(writer.stream_position()?); let mut extra_data_end = header_end + extensions.extra_data.len() as u64; if options.alignment > 1 { @@ -2633,12 +2631,25 @@ mod test { #[cfg(feature = "deflate64")] #[test] fn test_fuzz_crash_2024_06_13a() -> ZipResult<()> { - use CompressionMethod::Deflate64; use crate::write::ExtendedFileOptions; + use CompressionMethod::Deflate64; let mut writer = ZipWriter::new(Cursor::new(Vec::new())); writer.set_flush_on_finish_file(false); - let options = FileOptions { compression_method: Deflate64, compression_level: None, last_modified_time: DateTime::from_date_and_time(2039, 4, 17, 6, 18, 19)?, permissions: None, large_file: true, encrypt_with: None, extended_options: ExtendedFileOptions {extra_data: vec![].into(), central_extra_data: vec![].into()}, alignment: 4, ..Default::default() }; + let options = FileOptions { + compression_method: Deflate64, + compression_level: None, + last_modified_time: DateTime::from_date_and_time(2039, 4, 17, 6, 18, 19)?, + permissions: None, + large_file: true, + encrypt_with: None, + extended_options: ExtendedFileOptions { + extra_data: vec![].into(), + central_extra_data: vec![].into(), + }, + alignment: 4, + ..Default::default() + }; writer.add_directory_from_path("", options)?; let _ = writer.finish_into_readable()?; Ok(()) @@ -2652,7 +2663,20 @@ mod test { let sub_writer = { let mut writer = ZipWriter::new(Cursor::new(Vec::new())); writer.set_flush_on_finish_file(false); - let options = FileOptions { compression_method: Stored, compression_level: None, last_modified_time: DateTime::from_date_and_time(1980, 4, 14, 6, 11, 54)?, permissions: None, large_file: false, encrypt_with: None, extended_options: ExtendedFileOptions {extra_data: vec![].into(), central_extra_data: vec![].into()}, alignment: 185, ..Default::default() }; + let options = FileOptions { + compression_method: Stored, + compression_level: None, + last_modified_time: DateTime::from_date_and_time(1980, 4, 14, 6, 11, 54)?, + permissions: None, + large_file: false, + encrypt_with: None, + extended_options: ExtendedFileOptions { + extra_data: vec![].into(), + central_extra_data: vec![].into(), + }, + alignment: 185, + ..Default::default() + }; writer.add_symlink_from_path("", "", options)?; writer }; diff --git a/tests/end_to_end.rs b/tests/end_to_end.rs index 7a2c0872..7943a2ac 100644 --- a/tests/end_to_end.rs +++ b/tests/end_to_end.rs @@ -128,7 +128,9 @@ fn write_test_archive(file: &mut Cursor>, method: CompressionMethod, sha zip.start_file("test/☃.txt", options.clone()).unwrap(); zip.write_all(b"Hello, World!\n").unwrap(); - options.add_extra_data(0xbeef, EXTRA_DATA.to_owned().into_boxed_slice(), false).unwrap(); + options + .add_extra_data(0xbeef, EXTRA_DATA.to_owned().into_boxed_slice(), false) + .unwrap(); zip.start_file("test_with_extra_data/🐢.txt", options) .unwrap();