diff --git a/fuzz/fuzz_targets/encode_rgb_custom_q_table.rs b/fuzz/fuzz_targets/encode_rgb_custom_q_table.rs index 02ce621..d77793e 100644 --- a/fuzz/fuzz_targets/encode_rgb_custom_q_table.rs +++ b/fuzz/fuzz_targets/encode_rgb_custom_q_table.rs @@ -17,7 +17,7 @@ fuzz_target!(|data: &[u8]| { if width >0 && width < u16::MAX && height >0 && height < u16::MAX { let mut table = [0;64]; for i in 0..64 { - let v = q_table[i*2] as u16 + (q_table[i*2+1] as u16) << 8; + let v = u16::from_le_bytes([q_table[i * 2], q_table[i * 2 + 1]]); table[i] = v.max(1); } diff --git a/src/avx2.rs b/src/avx2.rs index f8eb103..8f1633b 100644 --- a/src/avx2.rs +++ b/src/avx2.rs @@ -10,6 +10,7 @@ pub(crate) struct AVX2Operations; impl Operations for AVX2Operations { #[inline(always)] fn fdct(data: &mut AlignedBlock) { - fdct_avx2(data); + // SAFETY: the encoder only selects AVX2Operations after is_x86_feature_detected!("avx2"). + unsafe { fdct_avx2(data) }; } } diff --git a/src/avx2/fdct.rs b/src/avx2/fdct.rs index c14f9be..c0d7667 100644 --- a/src/avx2/fdct.rs +++ b/src/avx2/fdct.rs @@ -58,15 +58,12 @@ const F_3_072: i16 = 25172; const DESCALE_P1: i32 = CONST_BITS - PASS1_BITS; const DESCALE_P2: i32 = CONST_BITS + PASS1_BITS; -#[inline(always)] -pub fn fdct_avx2(data: &mut AlignedBlock) { - unsafe { - fdct_avx2_internal(data); - } -} - +/// Forward DCT using AVX2. +/// +/// Callers outside an AVX2 context must check `is_x86_feature_detected!("avx2")` first, +/// which `#[target_feature]` enforces by making the call `unsafe` there. #[target_feature(enable = "avx2")] -fn fdct_avx2_internal(data: &mut AlignedBlock) { +pub fn fdct_avx2(data: &mut AlignedBlock) { #[target_feature(enable = "avx2")] #[allow(non_snake_case)] #[inline] @@ -500,7 +497,8 @@ mod tests { crate::fdct::fdct_scalar(&mut expected); let mut actual = AlignedBlock::new(input); - fdct_avx2(&mut actual); + // SAFETY: AVX2 support was checked above. + unsafe { fdct_avx2(&mut actual) }; assert_eq!(actual.data, expected.data); } diff --git a/src/avx2/ycbcr.rs b/src/avx2/ycbcr.rs index 9df37f3..717ade0 100644 --- a/src/avx2/ycbcr.rs +++ b/src/avx2/ycbcr.rs @@ -140,6 +140,12 @@ macro_rules! ycbcr_image_avx2 { #[inline(always)] fn fill_buffers(&self, y: u16, buffers: &mut [Vec; 4]) { + // The type is public (benchmark feature), so a safe call must not run AVX2 blindly. + assert!( + std::is_x86_feature_detected!("avx2"), + "AVX2 is not available" + ); + // SAFETY: AVX2 support was checked above. unsafe { self.fill_buffers_avx2(y, buffers); } diff --git a/src/encoder.rs b/src/encoder.rs index 5f41217..7cf1642 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -475,7 +475,11 @@ impl Encoder { height: u16, color_type: ColorType, ) -> Result<(), EncodingError> { - let required_data_len = width as usize * height as usize * color_type.get_bytes_per_pixel(); + // Saturate so 32-bit targets report an error instead of wrapping past the check. + let required_data_len = (width as usize) + .checked_mul(height as usize) + .and_then(|n| n.checked_mul(color_type.get_bytes_per_pixel())) + .unwrap_or(usize::MAX); if data.len() < required_data_len { return Err(EncodingError::BadImageData {