From 568fcab196fac0e4bb71a6a667a8f64e6f9de3b3 Mon Sep 17 00:00:00 2001 From: Danny McClanahan <1305167+cosmicexplorer@users.noreply.github.com> Date: Sun, 11 Aug 2024 08:59:18 -0400 Subject: [PATCH] add new read API to fuzzer --- fuzz/fuzz_targets/fuzz_read.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/fuzz/fuzz_targets/fuzz_read.rs b/fuzz/fuzz_targets/fuzz_read.rs index 78fe670ec..3622646bd 100644 --- a/fuzz/fuzz_targets/fuzz_read.rs +++ b/fuzz/fuzz_targets/fuzz_read.rs @@ -1,9 +1,12 @@ #![no_main] use libfuzzer_sys::fuzz_target; -use std::io::{Read, Seek, SeekFrom}; use tikv_jemallocator::Jemalloc; + +use std::io::prelude::*; + use zip::read::read_zipfile_from_stream; +use zip::unstable::read::streaming::StreamingArchive; const MAX_BYTES_TO_READ: u64 = 1 << 24; @@ -19,13 +22,34 @@ fn decompress_all(data: &[u8]) -> Result<(), Box> { std::io::copy(&mut file, &mut std::io::sink())?; } let mut reader = zip.into_inner(); - reader.seek(SeekFrom::Start(0))?; + reader.rewind()?; while let Ok(Some(mut file)) = read_zipfile_from_stream(&mut reader) { std::io::copy(&mut file, &mut std::io::sink())?; } Ok(()) } +fn decompress_generic(data: &[u8]) -> Result<(), Box> { + let reader = std::io::Cursor::new(data); + let mut zip = zip::ZipArchive::new(reader)?; + + for i in 0..zip.len() { + let mut file = zip.by_index_generic(i)?.take(MAX_BYTES_TO_READ); + std::io::copy(&mut file, &mut std::io::sink())?; + } + + let mut reader = zip.into_inner(); + reader.rewind()?; + let mut stream_zip = StreamingArchive::new(reader); + + while let Some(mut file) = stream_zip.next_entry()? { + std::io::copy(&mut file, &mut std::io::sink())?; + } + while let Some(_) = stream_zip.next_metadata_entry()? {} + Ok(()) +} + fuzz_target!(|data: &[u8]| { let _ = decompress_all(data); + let _ = decompress_generic(data); });