Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/encode_rgb_custom_q_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion src/avx2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) };
}
}
16 changes: 7 additions & 9 deletions src/avx2/fdct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 6 additions & 0 deletions src/avx2/ycbcr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ macro_rules! ycbcr_image_avx2 {

#[inline(always)]
fn fill_buffers(&self, y: u16, buffers: &mut [Vec<u8>; 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);
}
Expand Down
6 changes: 5 additions & 1 deletion src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,11 @@ impl<W: JfifWrite> Encoder<W> {
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 {
Expand Down