Skip to content

Commit

Permalink
add new read API to fuzzer
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicexplorer committed Aug 11, 2024
1 parent 4af99cc commit 568fcab
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions fuzz/fuzz_targets/fuzz_read.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -19,13 +22,34 @@ fn decompress_all(data: &[u8]) -> Result<(), Box<dyn std::error::Error>> {
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<dyn std::error::Error>> {
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);
});

0 comments on commit 568fcab

Please sign in to comment.