Skip to content

Commit ca2b708

Browse files
author
Amrit kumar Mahto
committed
fix: prevent MP4 & PS demuxer panics due to out-of-bounds/underflow (#1995)
1 parent bce0c92 commit ca2b708

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/rust/src/demuxer/stream_functions.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,9 @@ unsafe fn detect_stream_type_common(ctx: &mut CcxDemuxer, ccx_options: &mut Opti
331331
}
332332

333333
// Now check for PS (Needs PACK header)
334+
// We use saturating_sub to avoid underflow if the buffer is tiny.
334335
let limit = if ctx.startbytes_avail < 50000 {
335-
ctx.startbytes_avail - 3
336+
ctx.startbytes_avail.saturating_sub(3)
336337
} else {
337338
49997
338339
} as usize;
@@ -427,8 +428,10 @@ pub fn is_valid_mp4_box(
427428
)
428429
);
429430

430-
// If the box type is "moov", check if it contains a valid movie header (mvhd)
431+
// If the box type is "moov", we need to check if it contains "mvhd".
432+
// We must check the buffer length first to avoid an out-of-bounds panic.
431433
if idx == 2
434+
&& position + 15 < buffer.len()
432435
&& !(buffer[position + 12] == b'm'
433436
&& buffer[position + 13] == b'v'
434437
&& buffer[position + 14] == b'h'

0 commit comments

Comments
 (0)