Skip to content

Commit

Permalink
Bounds checks for bistream_restriction fields
Browse files Browse the repository at this point in the history
Fixes #55
  • Loading branch information
dholroyd committed Feb 26, 2024
1 parent 5670413 commit 549d4e6
Show file tree
Hide file tree
Showing 3 changed files with 442 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ hex-slice = "0.1.4"
memchr = "2.1.1"
rfc6381-codec = "0.1"
log = "0.4"
lazy_static = "1.4"

[dev-dependencies]
hex-literal = "0.4.1"
Expand Down
40 changes: 40 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![forbid(unsafe_code)]
#![deny(rust_2018_idioms)]

use crate::rbsp::BitReaderError;
use std::fmt::Debug;

pub mod annexb;
Expand Down Expand Up @@ -85,6 +86,45 @@ impl<T: Debug> Debug for ParamSetMap<T> {
}
}

#[derive(Debug)]
pub enum FieldErr {
TooLarge { value: u32, max_value: u32 },
TooSmall { value: u32, min_value: u32 },
BitReader(BitReaderError),
}

trait U32Field {
fn max(self, max_value: u32) -> Result<u32, FieldErr>;
fn min(self, min_value: u32) -> Result<u32, FieldErr>;
}
impl U32Field for Result<u32, BitReaderError> {
fn max(self, max_value: u32) -> Result<u32, FieldErr> {
match self {
Ok(value) => {
if value > max_value {
Err(FieldErr::TooLarge { value, max_value })
} else {
Ok(value)
}
}
Err(e) => Err(FieldErr::BitReader(e)),
}
}

fn min(self, min_value: u32) -> Result<u32, FieldErr> {
match self {
Ok(value) => {
if value < min_value {
Err(FieldErr::TooSmall { value, min_value })
} else {
Ok(value)
}
}
Err(e) => Err(FieldErr::BitReader(e)),
}
}
}

#[cfg(test)]
mod tests {
#[test]
Expand Down
Loading

0 comments on commit 549d4e6

Please sign in to comment.