From 9c0e544e79a4f7874dab449674a11d899bf61963 Mon Sep 17 00:00:00 2001 From: Igor Tolmachev Date: Tue, 16 Jul 2024 20:08:01 +0900 Subject: Add tests and fix bugs --- src/zip/driver.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/zip/driver.rs') diff --git a/src/zip/driver.rs b/src/zip/driver.rs index 62da39f..631c4ed 100644 --- a/src/zip/driver.rs +++ b/src/zip/driver.rs @@ -68,23 +68,23 @@ impl ArchiveRead for Zip { let limit = 65557.min(io.seek(SeekFrom::End(0))?) as i64; let start = io.seek(SeekFrom::End(-limit))?; let pos = start - + io.read_vec(limit as usize - 18)? - .windows(4) - .rposition(|v| u32::from_le_bytes(v.try_into().unwrap()) == 0x06054b50) - .ok_or(ZipError::EOCDRNotFound)? as u64; + + io.read_vec( + (limit as usize) + .checked_sub(18) + .ok_or(ZipError::EocdrNotFound)?, + )? + .windows(4) + .rposition(|v| u32::from_le_bytes(v.try_into().unwrap()) == 0x06054b50) + .ok_or(ZipError::EocdrNotFound)? as u64; // Read eocdr io.seek(SeekFrom::Start(pos + 4))?; let buf = io.read_arr::<18>()?; let eocdr: Eocdr = deserialize(&buf).unwrap(); - let comment = { - let mut buf: Vec = vec![0; eocdr.comment_len as usize]; - io.read(&mut buf)?; - String::from_cp437(buf) - }; + let comment = String::from_cp437(io.read_vec(eocdr.comment_len as usize)?); // Try to find eocdr64locator - io.seek(SeekFrom::Start(pos - 20))?; + io.seek(SeekFrom::Start(pos.saturating_sub(20)))?; let buf = io.read_arr::<20>()?; let (cd_pointer, cd_size, cd_records) = // If locator found then read eocdr64 -- cgit v1.2.3