forge
Explore
Log in

Don't you dare go Hollow

B1: ec-probe input bugs (AAC AVI sample count, Xing underflow, Ogg comment offset, window/underflow saturation, FLAC block scan bound and length clamp, WAV A/mu-law block_align 0, duration sample_rate 0 panic, TRCK first-wins)

Browse files at this commit

ede1525ff4954ad522fabe32ac79dea433b5486bAhmet Kaan GümüşParent: 22f0f3f
AVI AAC counted raw_blocks+1 blocks per frame; raw_blocks already holds the count, so it now uses adts_samples (1024 * raw_blocks), matching the ADTS path. AAC-in-AVI pts/dur only changes (halved).
Other fixes only change behaviour on malformed or edge inputs (no panic/underflow) and are not golden-visible on valid fixtures.
crates/ec-probe/src/decoder.rsmodified+2 -1
@@ -353,7 +353,8 @@ fn flac_stream_info(data: &[u8]) -> Option<&[u8]> {
353353 if data.len() == 34 {
354354 return Some(data);
355355 }
356- for at in 0..data.len().saturating_sub(38).min(16) {
356+ let last = data.len().checked_sub(38)?;
357+ for at in 0..=last.min(16) {
357358 let head = &data[at..at + 4];
358359 let len = u32::from_be_bytes([0, head[1], head[2], head[3]]);
359360 if head[0] & 0x7f == 0 && len == 34 {
crates/ec-probe/src/lib.rsmodified+21 -19
@@ -315,7 +315,12 @@ impl Reader {
315315 .find(|s| s.params.codec == CodecId::Vorbis)
316316 .and_then(|s| s.params.extradata.as_deref())
317317 .and_then(|data| ec_ogg::xiph_unlace(data).ok())
318- .and_then(|packets| packets.get(1).map(|p| tags::from_vorbis_comment(&p[7..])))
318+ .and_then(|packets| {
319+ packets
320+ .get(1)
321+ .and_then(|p| p.get(7..))
322+ .map(tags::from_vorbis_comment)
323+ })
319324 .unwrap_or_default();
320325 let native = (0..demuxer.streams().len() as u64).collect();
321326 Ok(Reader::wrap(format, Box::new(demuxer), tags, native))
@@ -407,7 +412,7 @@ impl Reader {
407412 let padding_ticks = s
408413 .params
409414 .audio()
410- .filter(|_| s.initial_padding > 0)
415+ .filter(|a| a.sample_rate > 0 && s.initial_padding > 0)
411416 .map(|a| {
412417 TimeBase::from_rate(a.sample_rate).rescale(
413418 i64::from(s.initial_padding),
@@ -416,14 +421,10 @@ impl Reader {
416421 )
417422 })
418423 .unwrap_or(0);
419- Timestamp::new(d - padding_ticks, s.time_base)
424+ Timestamp::new(d.saturating_sub(padding_ticks), s.time_base)
420425 })
421426 })
422- .max_by(|a, b| {
423- a.as_secs_f64()
424- .partial_cmp(&b.as_secs_f64())
425- .unwrap_or(std::cmp::Ordering::Equal)
426- })
427+ .max()
427428 }
428429
429430 /// Every audio stream with no decoder behind it, and why.
@@ -559,7 +560,7 @@ fn open_dts_span<R: Read + Seek + Send + 'static>(
559560 start: u64,
560561 end: u64,
561562 ) -> Result<Reader> {
562- let mut window = vec![0u8; HEAD.min((end - start) as usize)];
563+ let mut window = vec![0u8; HEAD.min(end.saturating_sub(start) as usize)];
563564 src.seek(SeekFrom::Start(start))?;
564565 let got = read_upto(&mut src, &mut window)?;
565566 window.truncate(got);
@@ -655,7 +656,8 @@ fn open_flac<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64)
655656 }
656657 let last = header[0] & 0x80 != 0;
657658 let kind = header[0] & 0x7f;
658- let len = u64::from(u32::from_be_bytes([0, header[1], header[2], header[3]])).min(1 << 24);
659+ let len = u64::from(u32::from_be_bytes([0, header[1], header[2], header[3]]))
660+ .min(end.saturating_sub(at + 4));
659661 let mut body = vec![0u8; len as usize];
660662 read_upto(&mut src, &mut body)?;
661663 at += 4 + len;
@@ -680,7 +682,7 @@ fn open_flac<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64)
680682
681683 fn open_mp3<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64) -> Result<Reader> {
682684 let (start, tags) = id3(&mut src, head, end)?;
683- let mut window = vec![0u8; HEAD.min((end - start) as usize)];
685+ let mut window = vec![0u8; HEAD.min(end.saturating_sub(start) as usize)];
684686 src.seek(SeekFrom::Start(start))?;
685687 let got = read_upto(&mut src, &mut window)?;
686688 window.truncate(got);
@@ -688,7 +690,7 @@ fn open_mp3<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64)
688690 .ok_or_else(|| Error::corrupt("mp3: no frame in the first 64 KiB"))?;
689691 let header = ec_mp3::FrameHeader::parse(&window[at..])?;
690692 let mut start = start + at as u64;
691- let audio_end = end.saturating_sub(id3v1_len(&mut src, end)?);
693+ let audio_end = end.saturating_sub(id3v1_len(&mut src, end)?).max(start);
692694 // A Xing/Info header states the frame count exactly; without one the
693695 // duration is the bitrate estimate the oracle also falls back to, which is
694696 // right for CBR and approximate for VBR.
@@ -708,7 +710,10 @@ fn open_mp3<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64)
708710 let (padding, total) = match &xing {
709711 Some(x) => (
710712 x.delay + MP3_DECODER_DELAY,
711- Some(x.frames * per_frame - u64::from(x.padding.saturating_sub(MP3_DECODER_DELAY))),
713+ Some(
714+ (x.frames * per_frame)
715+ .saturating_sub(u64::from(x.padding.saturating_sub(MP3_DECODER_DELAY))),
716+ ),
712717 ),
713718 None => (
714719 0,
@@ -732,7 +737,7 @@ fn open_mp3<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64)
732737
733738 fn open_adts<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64) -> Result<Reader> {
734739 let (start, tags) = id3(&mut src, head, end)?;
735- let mut window = vec![0u8; HEAD.min((end - start) as usize)];
740+ let mut window = vec![0u8; HEAD.min(end.saturating_sub(start) as usize)];
736741 src.seek(SeekFrom::Start(start))?;
737742 let got = read_upto(&mut src, &mut window)?;
738743 window.truncate(got);
@@ -740,7 +745,7 @@ fn open_adts<R: Read + Seek + Send + 'static>(mut src: R, head: &[u8], end: u64)
740745 .ok_or_else(|| Error::corrupt("aac: no ADTS frame in the first 64 KiB"))?;
741746 let header = ec_aac::parse_adts(&window[at..])?;
742747 let start = start + at as u64;
743- let audio_end = end.saturating_sub(id3v1_len(&mut src, end)?);
748+ let audio_end = end.saturating_sub(id3v1_len(&mut src, end)?).max(start);
744749 // ADTS states no duration anywhere: every frame is 1024 samples, so the
745750 // file's size over its *average* frame is the estimate — the same one
746751 // the oracle prints "estimating duration from bitrate" about. Averaged rather
@@ -1075,10 +1080,7 @@ fn avi_elementary_frame(codec: CodecId, data: &[u8]) -> Result<Option<(usize, u6
10751080 return Ok(None);
10761081 }
10771082 let header = ec_aac::parse_adts(data)?;
1078- Ok(Some((
1079- header.frame_length,
1080- (u64::from(header.raw_blocks) + 1) * ec_aac::FRAME_LEN as u64,
1081- )))
1083+ Ok(Some((header.frame_length, raw::adts_samples(&header))))
10821084 }
10831085 _ => Ok(None),
10841086 }
crates/ec-probe/src/raw.rsmodified+8 -3
@@ -293,7 +293,7 @@ fn frame_at(kind: Kind, data: &[u8], flac: Option<&ec_flac::StreamInfo>) -> Resu
293293 }
294294 Ok(Framing {
295295 bytes: header.frame_length,
296- samples: 1024 * u64::from(header.raw_blocks),
296+ samples: adts_samples(&header),
297297 })
298298 }
299299 Kind::Dts => {
@@ -362,7 +362,7 @@ impl<R: Read + Seek + Send> Demuxer for RawDemuxer<R> {
362362 // PCM needs no index and no rounding: every sample is a random access
363363 // point, at a byte offset arithmetic gives.
364364 if let Kind::Wav { block_align } = self.kind {
365- let at = self.start + target * block_align as u64;
365+ let at = self.start + target.saturating_mul(block_align as u64);
366366 self.jump(at.min(self.end))?;
367367 self.sample = target;
368368 return Ok(());
@@ -413,7 +413,7 @@ pub(crate) fn wav_compressed_parameters(
413413 }
414414 let kind = match c.tag {
415415 0x0006 | 0x0007 => Kind::Wav {
416- block_align: usize::from(spec.channels),
416+ block_align: usize::from(spec.channels.max(1)),
417417 },
418418 _ => Kind::WavBlocks {
419419 block: usize::from(c.block_align),
@@ -451,6 +451,11 @@ pub(crate) fn wav_parameters(spec: ec_riff::WavSpec) -> Result<(CodecParameters,
451451 Ok((params, spec.block_align()))
452452 }
453453
454+/// Samples one ADTS frame decodes to: `raw_blocks` already counts the blocks.
455+pub(crate) fn adts_samples(header: &ec_aac::AdtsHeader) -> u64 {
456+ u64::from(header.raw_blocks) * ec_aac::FRAME_LEN as u64
457+}
458+
454459 /// Codec parameters for an ADTS stream, from its first frame header.
455460 pub(crate) fn adts_parameters(header: &ec_aac::AdtsHeader) -> CodecParameters {
456461 let mut params = CodecParameters::new(CodecId::Aac);
crates/ec-probe/src/tags.rsmodified+2 -1
@@ -50,10 +50,11 @@ impl Tags {
5050 "TRCK" | "TRK" | "TRACKNUMBER" => {
5151 // "3/12" is a track number and a total; the number is the part
5252 // anything displays.
53- self.track = value
53+ let track = value
5454 .split(['/', ' '])
5555 .next()
5656 .and_then(|n| n.trim().parse().ok());
57+ self.track = self.track.or(track);
5758 return;
5859 }
5960 _ => return,