From 6157daa468bc2d5c332c055570bfddefce3f5a3b Mon Sep 17 00:00:00 2001 From: Takeru Ohta Date: Mon, 2 Jul 2018 02:50:01 +0900 Subject: [PATCH] Return io::Error if failed to decode distance code width table This resolves RazrFalcon/usvg#21 --- src/deflate/symbol.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/deflate/symbol.rs b/src/deflate/symbol.rs index 5830e77..1c670a5 100644 --- a/src/deflate/symbol.rs +++ b/src/deflate/symbol.rs @@ -399,7 +399,14 @@ impl HuffmanCodec for DynamicHuffmanCodec { .or_else(|| literal_code_bitwidthes.last().cloned()); distance_code_bitwidthes.extend(load_bitwidthes(reader, c, last)?); } - debug_assert_eq!(distance_code_bitwidthes.len(), distance_code_count as usize); + if distance_code_bitwidthes.len() > distance_code_count as usize { + let message = format!( + "The length of `distance_code_bitwidthes` is too large: actual={}, expected={}", + distance_code_bitwidthes.len(), + distance_code_count + ); + return Err(io::Error::new(io::ErrorKind::InvalidData, message)); + } Ok(Decoder { literal: huffman::DecoderBuilder::from_bitwidthes(