Skip to content

Commit 568fcab

Browse files
add new read API to fuzzer
1 parent 4af99cc commit 568fcab

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

fuzz/fuzz_targets/fuzz_read.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#![no_main]
22

33
use libfuzzer_sys::fuzz_target;
4-
use std::io::{Read, Seek, SeekFrom};
54
use tikv_jemallocator::Jemalloc;
5+
6+
use std::io::prelude::*;
7+
68
use zip::read::read_zipfile_from_stream;
9+
use zip::unstable::read::streaming::StreamingArchive;
710

811
const MAX_BYTES_TO_READ: u64 = 1 << 24;
912

@@ -19,13 +22,34 @@ fn decompress_all(data: &[u8]) -> Result<(), Box<dyn std::error::Error>> {
1922
std::io::copy(&mut file, &mut std::io::sink())?;
2023
}
2124
let mut reader = zip.into_inner();
22-
reader.seek(SeekFrom::Start(0))?;
25+
reader.rewind()?;
2326
while let Ok(Some(mut file)) = read_zipfile_from_stream(&mut reader) {
2427
std::io::copy(&mut file, &mut std::io::sink())?;
2528
}
2629
Ok(())
2730
}
2831

32+
fn decompress_generic(data: &[u8]) -> Result<(), Box<dyn std::error::Error>> {
33+
let reader = std::io::Cursor::new(data);
34+
let mut zip = zip::ZipArchive::new(reader)?;
35+
36+
for i in 0..zip.len() {
37+
let mut file = zip.by_index_generic(i)?.take(MAX_BYTES_TO_READ);
38+
std::io::copy(&mut file, &mut std::io::sink())?;
39+
}
40+
41+
let mut reader = zip.into_inner();
42+
reader.rewind()?;
43+
let mut stream_zip = StreamingArchive::new(reader);
44+
45+
while let Some(mut file) = stream_zip.next_entry()? {
46+
std::io::copy(&mut file, &mut std::io::sink())?;
47+
}
48+
while let Some(_) = stream_zip.next_metadata_entry()? {}
49+
Ok(())
50+
}
51+
2952
fuzz_target!(|data: &[u8]| {
3053
let _ = decompress_all(data);
54+
let _ = decompress_generic(data);
3155
});

0 commit comments

Comments
 (0)