Fix read in packbits decoder

This assumed that a `read` would succeed for the full length, thus
interpreting in a subsequent call some data bits as a header.

For instance, consider 77 bytes of literal were left and a buffer of
1024 bytes was provided. Then the `read` call might return 60 which was
ignored and assumed to be 77. Thus the next 17 bytes of literal data
were instead interpreted as a header due to setting `self.count` to 0.
This commit is contained in:
Andreas Molzer committed 2022-12-03 18:42:08 +01:00
1 parent 65bc24d5d9
commit 1f7b25ef55
1 file changed
+7 -7
+7 -7
View File
@@ -384,23 +384,23 @@ impl<R: Read> Read for PackBitsReader<R> {
} }
let length = buf.len().min(self.count); let length = buf.len().min(self.count);
match self.state { let actual = match self.state {
PackBitsReaderState::Literal => { PackBitsReaderState::Literal => self.reader.read(&mut buf[..length])?,
self.reader.read(&mut buf[..length])?;
}
PackBitsReaderState::Repeat { value } => { PackBitsReaderState::Repeat { value } => {
for b in &mut buf[..length] { for b in &mut buf[..length] {
*b = value; *b = value;
} }
length
} }
PackBitsReaderState::Header => unreachable!(), PackBitsReaderState::Header => unreachable!(),
} };
self.count -= length; self.count -= actual;
if self.count == 0 { if self.count == 0 {
self.state = PackBitsReaderState::Header; self.state = PackBitsReaderState::Header;
} }
return Ok(length); return Ok(actual);
} }
} }