Skip to content

Commit a0700f4

Browse files
committed
Fix skipping of extra LZW data
1 parent 27ea2f4 commit a0700f4

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/reader/decoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ impl StreamingDecoder {
772772
if left > 0 {
773773
let n = cmp::min(left, buf.len());
774774
if self.lzw_reader.has_ended() || matches!(write_into, OutputBuffer::None) {
775-
return goto!(n, DecodeSubBlock(0), emit Decoded::Nothing);
775+
return goto!(n, DecodeSubBlock(left - n), emit Decoded::Nothing);
776776
}
777777

778778
let (mut consumed, bytes_len) = self.lzw_reader.decode_bytes(&buf[..n], write_into)?;

tests/stall.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![cfg(feature = "std")]
22

3-
use std::{fs, sync::mpsc, thread, time::Duration};
3+
use std::{fs, sync::mpsc, thread, time::Duration, io};
44

55
#[test]
66
fn try_decode_crash_regression() {
@@ -42,3 +42,38 @@ fn decode(data: &[u8]) -> Result<(), gif::DecodingError> {
4242

4343
Ok(())
4444
}
45+
46+
#[test]
47+
fn one_byte_at_a_time() {
48+
let r = OneByte {
49+
data: include_bytes!("../tests/samples/moon_impact.gif"),
50+
};
51+
let frames = gif::DecodeOptions::new().read_info(r).unwrap()
52+
.into_iter().enumerate().map(|(n, f)| {
53+
f.expect(&n.to_string())
54+
}).count();
55+
assert_eq!(frames, 14);
56+
}
57+
58+
struct OneByte<'a> {
59+
data: &'a [u8],
60+
}
61+
62+
impl io::BufRead for OneByte<'_> {
63+
fn fill_buf(&mut self) -> io::Result<&[u8]> {
64+
Ok(&self.data[..self.data.len().min(1)])
65+
}
66+
fn consume(&mut self, n: usize) {
67+
debug_assert!(n <= 1);
68+
self.data = &self.data[n..];
69+
}
70+
}
71+
72+
impl io::Read for OneByte<'_> {
73+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
74+
let n = self.data.len().min(buf.len()).min(1);
75+
buf[..n].copy_from_slice(&self.data[..n]);
76+
self.data = &self.data[n..];
77+
Ok(n)
78+
}
79+
}

0 commit comments

Comments
 (0)