diff --git a/crates/video/src/theora/encoder.rs b/crates/video/src/theora/encoder.rs index 407225e6e5..7e1eb82d05 100644 --- a/crates/video/src/theora/encoder.rs +++ b/crates/video/src/theora/encoder.rs @@ -104,9 +104,16 @@ fn alloc_vec_exact(val: T, n: usize) -> Result> { impl Encoder { /// Create a new encoder with the given configuration. pub fn new(config: EncoderConfig) -> Result { - if config.width == 0 || config.height == 0 { + if config.width == 0 || config.height == 0 || config.pic_width == 0 || config.pic_height == 0 { return Err(Error::DimensionMismatch("dimensions cannot be zero")); } + if config.pic_x > 255 + || config.pic_y > 255 + || config.pic_x.checked_add(config.pic_width).is_none_or(|end| end > config.width) + || config.pic_y.checked_add(config.pic_height).is_none_or(|end| end > config.height) + { + return Err(Error::DimensionMismatch("picture region exceeds frame bounds")); + } let frame_width = config.width.checked_add(15).map(|w| w & !15).ok_or(Error::DimensionMismatch("width overflows"))?; let frame_height = config.height.checked_add(15).map(|h| h & !15).ok_or(Error::DimensionMismatch("height overflows"))?; let fmbw_u64 = (frame_width / 16) as u64; diff --git a/crates/video/tests/theora.rs b/crates/video/tests/theora.rs index 2c57b510e6..eadc09d29d 100644 --- a/crates/video/tests/theora.rs +++ b/crates/video/tests/theora.rs @@ -608,6 +608,39 @@ fn test_theora_encoder_invalid_input_rejected() { let mut pic = test_picture(32, 32); pic.y_stride = 16; assert!(matches!(enc.encode_frame(&pic, true), Err(Error::DimensionMismatch(_)))); + + // Out-of-frame configuration picture bounds cannot bypass active picture validation + let cfg_out_of_frame = EncoderConfig { + width: 32, + height: 32, + pic_width: 16, + pic_height: 32, + pic_x: 32, + pic_y: 0, + fps_num: 25, + fps_den: 1, + pixel_format: PixelFormat::Yuv420, + color_space: ColorSpace::Undefined, + quality: 40, + keyframe_distance: 64, + }; + assert!(matches!(Encoder::new(cfg_out_of_frame), Err(Error::DimensionMismatch(_)))); + + let cfg_zero_pic = EncoderConfig { + width: 32, + height: 32, + pic_width: 0, + pic_height: 32, + pic_x: 0, + pic_y: 0, + fps_num: 25, + fps_den: 1, + pixel_format: PixelFormat::Yuv420, + color_space: ColorSpace::Undefined, + quality: 40, + keyframe_distance: 64, + }; + assert!(matches!(Encoder::new(cfg_zero_pic), Err(Error::DimensionMismatch(_)))); } #[test]