Skip to content

Commit 0db0c13

Browse files
authored
Merge pull request #2786 from image-rs/issue-2738-loop-count-panic
Fix loop count logic
2 parents a0f66c1 + 2335f47 commit 0db0c13

5 files changed

Lines changed: 28 additions & 12 deletions

File tree

‎src/codecs/gif.rs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,13 @@ impl<R: Read> Iterator for GifFrameIterator<R> {
426426
impl<'a, R: BufRead + Seek + 'a> AnimationDecoder<'a> for GifDecoder<R> {
427427
fn loop_count(&self) -> LoopCount {
428428
match self.reader.repeat() {
429-
gif::Repeat::Finite(n) => LoopCount::Finite(
430-
NonZeroU32::new(n.into()).expect("Repeat::Finite should be non-zero"),
431-
),
432-
gif::Repeat::Infinite => LoopCount::Infinite,
429+
gif::Repeat::Finite(n @ 1..) => {
430+
LoopCount::Finite(NonZeroU32::new(n.into()).expect("repeat is non-zero"))
431+
}
432+
gif::Repeat::Finite(0) | gif::Repeat::Infinite => LoopCount::Infinite,
433433
}
434434
}
435+
435436
fn into_frames(self) -> animation::Frames<'a> {
436437
animation::Frames::new(Box::new(GifFrameIterator::new(self)))
437438
}

‎src/codecs/png.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,10 +551,10 @@ impl<'a, R: BufRead + Seek + 'a> AnimationDecoder<'a> for ApngDecoder<R> {
551551
fn loop_count(&self) -> LoopCount {
552552
match self.inner.reader.info().animation_control() {
553553
None => LoopCount::Infinite,
554-
Some(actl) if actl.num_plays == 0 => LoopCount::Infinite,
555-
Some(actl) => LoopCount::Finite(
556-
NonZeroU32::new(actl.num_plays).expect("num_plays should be non-zero"),
557-
),
554+
Some(actl) => match NonZeroU32::new(actl.num_plays) {
555+
None => LoopCount::Infinite,
556+
Some(n) => LoopCount::Finite(n),
557+
},
558558
}
559559
}
560560

‎src/codecs/webp/decoder.rs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::io::{BufRead, Read, Seek};
2-
use std::num::NonZeroU32;
32

43
use image_webp::LoopCount;
54

@@ -106,9 +105,7 @@ impl<'a, R: 'a + BufRead + Seek> AnimationDecoder<'a> for WebPDecoder<R> {
106105
fn loop_count(&self) -> crate::metadata::LoopCount {
107106
match self.inner.loop_count() {
108107
LoopCount::Forever => crate::metadata::LoopCount::Infinite,
109-
LoopCount::Times(n) => crate::metadata::LoopCount::Finite(
110-
NonZeroU32::new(n.get().into()).expect("LoopCount::Times should be non-zero"),
111-
),
108+
LoopCount::Times(n) => crate::metadata::LoopCount::Finite(n.into()),
112109
}
113110
}
114111

‎tests/regression.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,21 @@ fn resizing_with_catmul() {
234234
resizable.resize_exact(nw, nh, FilterType::CatmullRom);
235235
assert_eq!(resizable, expected);
236236
}
237+
238+
#[test]
239+
#[cfg(feature = "gif")]
240+
fn gif_regressions() {
241+
use image::codecs::gif::GifDecoder;
242+
use image::AnimationDecoder as _;
243+
244+
let base: PathBuf = BASE_PATH.iter().collect();
245+
let path = base.join("regression/gif/zero-loop-count.gif");
246+
let file = BufReader::new(File::open(path).unwrap());
247+
248+
let decoder = GifDecoder::new(file).expect("Failed to create GIF decoder for regression test");
249+
250+
let _ = decoder.loop_count();
251+
let mut frames = decoder.into_frames();
252+
253+
while let Some(Ok(_frame)) = frames.next() {}
254+
}
15 Bytes
Loading

0 commit comments

Comments
 (0)