.cdr import: bytes a failing compressed stream inflated count toward the limit

A zlib stream that fails near its end has already inflated most of its
bytes, which the reader discarded without counting them. Several such
streams could each inflate up to the file's inflate limit. Those bytes
now count toward it.
This commit is contained in:
dotCooCoo
2026-11-12 13:40:48 -08:00
parent 08155e34e3
commit e71ab08233
2 changed files with 24 additions and 0 deletions
+4
View File
@@ -378,6 +378,10 @@ impl<'a> Index<'a> {
let got = match reader.take((left as u64).saturating_add(1)).read_to_end(&mut self.inflated) {
Ok(n) => n,
Err(_) => {
// What a stream inflated before it failed counts toward the limit too, so that
// streams that fail late can't inflate past it again and again.
let produced = self.inflated.len().saturating_sub(at);
self.left.set(left.saturating_sub(produced));
self.inflated.truncate(at);
return Err(Error::Damaged("compressed block"));
}
+20
View File
@@ -1319,6 +1319,26 @@ fn a_zip_bomb_stops_at_the_inflate_limit() {
assert_eq!(read(&file, limits), Err(Error::Limit("inflated size")));
}
/// What a compressed stream inflates before it fails counts toward the inflate limit, so that
/// streams failing at their checksum can't each inflate up to the limit.
#[test]
fn bytes_inflated_by_a_failing_stream_count_toward_the_limit() {
let failing = || {
let mut cmpr = synth::cmpr(&[chunk(b"junk", vec![0; 600 << 10])]);
if let synth::Node::Chunk(_, body) = &mut cmpr {
// The last byte of the first block's zlib stream is part of its checksum.
let first = u32::from_le_bytes(body[4..8].try_into().unwrap()) as usize;
body[4 + 16 + first - 1] ^= 0xff;
}
cmpr
};
let file = synth::riff(1100, &[chunk(b"vrsn", 1100u16.to_le_bytes().to_vec()), failing(), failing()]);
let one =
read(&synth::riff(1100, &[chunk(b"vrsn", 1100u16.to_le_bytes().to_vec()), failing()]), Limits { inflated: 1 << 20, ..Limits::default() });
assert!(one.is_ok_and(|d| d.warnings == vec![Warning::Damaged]));
assert_eq!(read(&file, Limits { inflated: 1 << 20, ..Limits::default() }), Err(Error::Limit("inflated size")));
}
#[test]
fn a_million_empty_chunks_stop_at_the_chunk_limit() {
let kids: Vec<_> = (0..5000).map(|_| chunk(b"junk", vec![])).collect();