Skip to content

Commit 7a10a83

Browse files
alfredoyangkinetiknz
authored andcommitted
check subtraction underflow
1 parent 2c9415c commit 7a10a83

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

mp4parse_capi/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -833,20 +833,23 @@ impl<'a> Iterator for SampleToChunkIterator<'a> {
833833
let has_chunk = self.chunks.next()
834834
.or_else(|| {
835835
self.chunks = match (self.stsc_peek_iter.next(), self.stsc_peek_iter.peek()) {
836-
(Some(next), Some(peek)) => {
836+
(Some(next), Some(peek)) if next.first_chunk > 0 && peek.first_chunk > 0 => {
837837
self.sample_count = next.samples_per_chunk;
838838
((next.first_chunk - 1) .. (peek.first_chunk - 1))
839839
},
840-
(Some(next), None) => {
840+
(Some(next), None) if next.first_chunk > 0 => {
841841
self.sample_count = next.samples_per_chunk;
842842
// Total chunk number in 'stsc' could be different to 'stco',
843843
// there could be more chunks at the last 'stsc' record.
844844
((next.first_chunk - 1) .. next.first_chunk + self.remain_chunk_count -1)
845845
},
846846
_ => (0 .. 0),
847847
};
848-
self.remain_chunk_count -= self.chunks.len() as u32;
849-
self.chunks.next()
848+
849+
self.remain_chunk_count.checked_sub(self.chunks.len() as u32).and_then(|res| {
850+
self.remain_chunk_count = res;
851+
self.chunks.next()
852+
})
850853
});
851854

852855
has_chunk.map_or(None, |id| { Some((id, self.sample_count)) })

0 commit comments

Comments
 (0)