Skip to content

Commit

Permalink
Fix read in packbits decoder
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
HeroicKatora committed Dec 3, 2022
1 parent 65bc24d commit 96dd18a
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/decoder/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,23 +384,25 @@ impl<R: Read> Read for PackBitsReader<R> {
}

let length = buf.len().min(self.count);
match self.state {
let actual = match self.state {
PackBitsReaderState::Literal => {
self.reader.read(&mut buf[..length])?;
self.reader.read(&mut buf[..length])?
}
PackBitsReaderState::Repeat { value } => {
for b in &mut buf[..length] {
*b = value;
}

length
}
PackBitsReaderState::Header => unreachable!(),
}
};

self.count -= length;
self.count -= actual;
if self.count == 0 {
self.state = PackBitsReaderState::Header;
}
return Ok(length);
return Ok(actual);
}
}

Expand Down

0 comments on commit 96dd18a

Please sign in to comment.