Skip to content

Commit

Permalink
fix & improve sps parse bound checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed Nov 6, 2024
1 parent 1a85e6b commit 383e6c6
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions crates/store/re_video/src/decode/ffmpeg_h264/sps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::decode::YuvPixelLayout;

use super::nalu::{NalHeader, NalUnitType};

#[derive(thiserror::Error, Debug)]
#[derive(thiserror::Error, Debug, PartialEq, Eq)]
pub enum SpsParsingError {
#[error("SPS buffer too small")]
UnexpectedEndOfSpsBuffer,
Expand Down Expand Up @@ -271,16 +271,13 @@ fn read_bits(
) -> Result<u32, SpsParsingError> {
debug_assert!(num_bits <= 32);

let byte_pos = *bit_read_pos / 8;
if buffer.len() <= byte_pos {
return Err(SpsParsingError::UnexpectedEndOfSpsBuffer);
}

let num_bytes = num_bits.next_multiple_of(8) / 8;
if buffer.len() < byte_pos + num_bytes {
let highest_byte_read = (num_bits + *bit_read_pos).next_multiple_of(8) / 8;
if buffer.len() < highest_byte_read {
return Err(SpsParsingError::UnexpectedEndOfSpsBuffer);
}

// Read bit by bit.
// Obviously this can be sped up by reading bytes when possible, but let's keep it simple.
let mut result = 0;
for n in 0..num_bits {
let bit_pos = *bit_read_pos + n;
Expand Down Expand Up @@ -318,6 +315,8 @@ fn read_exponential_golomb(

#[cfg(test)]
mod tests {
use crate::decode::ffmpeg_h264::sps::SpsParsingError;

use super::{read_bits, read_exponential_golomb};

#[test]
Expand All @@ -337,7 +336,14 @@ mod tests {
);
assert_eq!(bit_pos, 12);

assert_eq!(read_bits(&mut 0, &[0], 9), Err(()));
assert_eq!(
read_bits(&mut 0, &[0], 9),
Err(SpsParsingError::UnexpectedEndOfSpsBuffer)
);
assert_eq!(
read_bits(&mut 1, &[0], 8),
Err(SpsParsingError::UnexpectedEndOfSpsBuffer)
);
}

#[test]
Expand Down Expand Up @@ -369,5 +375,10 @@ mod tests {
6
);
assert_eq!(bit_pos, 7);

assert_eq!(
read_exponential_golomb(&mut 0, &[0b_0000_1111]),
Err(SpsParsingError::UnexpectedEndOfSpsBuffer)
);
}
}

0 comments on commit 383e6c6

Please sign in to comment.