diff --git a/crates/codecs/src/resolution.rs b/crates/codecs/src/resolution.rs index 8798829cf4..aa3d534cdd 100644 --- a/crates/codecs/src/resolution.rs +++ b/crates/codecs/src/resolution.rs @@ -526,7 +526,11 @@ fn exif_without_thumbnail(exif: &[u8]) -> Option> { } // Classic TIFF: also blank the thumbnail JPEG itself (IFD1's JPEGInterchangeFormat/Length), // which stays recoverable in the block once nothing points at it. - let thumb = (!big_tiff).then(|| thumbnail_range(order, body, next - prefix)).flatten(); + // Only a range clear of IFD0's table and values: one that covers them is not a thumbnail. + let thumb = (!big_tiff) + .then(|| thumbnail_range(order, body, next - prefix)) + .flatten() + .filter(|&(off, len)| !overlaps_ifd0(order, body, first - 2, count, off, len)); let mut out = exif.to_vec(); out.get_mut(next..next + width)?.fill(0); if let Some((off, len)) = thumb @@ -537,17 +541,48 @@ fn exif_without_thumbnail(exif: &[u8]) -> Option> { Some(out) } +/// Whether `off..off + len` of `body` covers part of classic IFD0 (`ifd0` is its entry count's +/// offset): its table with the next-IFD pointer, or an out-of-line value of one of its entries. +fn overlaps_ifd0(order: Order, body: &[u8], ifd0: usize, count: usize, off: usize, len: usize) -> bool { + let end = off.saturating_add(len); + let hits = |a: usize, size: usize| a < end && off < a.saturating_add(size); + if hits(ifd0, 2 + count * 12 + 4) { + return true; + } + (0..count).any(|i| { + let e = ifd0 + 2 + i * 12; + let unit = match order.u16(body, e + 2) { + Some(1 | 2 | 6 | 7) => 1, + Some(3 | 8) => 2, + Some(4 | 9 | 11) => 4, + Some(5 | 10 | 12) => 8, + _ => return false, + }; + let size = usize::try_from(order.u32(body, e + 4).unwrap_or(0)).unwrap_or(usize::MAX).saturating_mul(unit); + size > 4 && order.u32(body, e + 8).and_then(|v| usize::try_from(v).ok()).is_some_and(|v| hits(v, size)) + }) +} + /// IFD1's thumbnail JPEG as (offset, length) in `body`, from the next-IFD pointer at `ptr`. fn thumbnail_range(order: Order, body: &[u8], ptr: usize) -> Option<(usize, usize)> { let ifd1 = usize::try_from(order.u32(body, ptr)?).ok()?; let (mut off, mut len) = (None, None); for i in 0..usize::from(order.u16(body, ifd1)?) { let e = ifd1.checked_add(2)?.checked_add(i.checked_mul(12)?)?; - let value = if order.u16(body, e + 2)? == 3 { usize::from(order.u16(body, e + 8)?) } else { usize::try_from(order.u32(body, e + 8)?).ok()? }; - match order.u16(body, e)? { - 0x0201 => off = Some(value), - 0x0202 => len = Some(value), - _ => {} + let tag = order.u16(body, e)?; + if !matches!(tag, 0x0201 | 0x0202) { + continue; + } + // One SHORT or LONG value; anything else holds an unrelated number. + let ty = order.u16(body, e + 2)?; + if order.u32(body, e + 4)? != 1 || !matches!(ty, 3 | 4) { + return None; + } + let value = if ty == 3 { usize::from(order.u16(body, e + 8)?) } else { usize::try_from(order.u32(body, e + 8)?).ok()? }; + if tag == 0x0201 { + off = Some(value); + } else { + len = Some(value); } } Some((off?, len?)) diff --git a/crates/codecs/tests/it/resolution.rs b/crates/codecs/tests/it/resolution.rs index a2bd7e8502..c0634b7a79 100644 --- a/crates/codecs/tests/it/resolution.rs +++ b/crates/codecs/tests/it/resolution.rs @@ -536,6 +536,27 @@ fn exports_drop_the_source_exif_thumbnail() { } } +/// A thumbnail range that covers IFD0, or tags that are not one SHORT/LONG value, are not blanked. +#[test] +fn thumbnail_blanking_keeps_ifd0_and_ignores_malformed_tags() { + for e in [&LE, &BE] { + let exif = exif_with_thumbnail(e); + let ifd1 = camera_exif(e).len(); + let table = 8..8 + 2 + 4 * 12; + // Offset 8, length 60: the range covers IFD0's table. + let mut over = exif.clone(); + over[ifd1 + 2 + 8..ifd1 + 2 + 12].copy_from_slice(&e.u32(8)); + over[ifd1 + 2 + 12 + 8..ifd1 + 2 + 12 + 12].copy_from_slice(&e.u32(60)); + let out = export_exif(&over, None); + assert_eq!(next_ifd(e, &out), 0, "IFD1 is still unlinked"); + assert_eq!(out[table.start..table.end], over[table.start..table.end], "IFD0's table is untouched"); + // A count of 2 on the offset tag: the thumbnail bytes are not blanked. + let mut counted = exif.clone(); + counted[ifd1 + 2 + 4..ifd1 + 2 + 8].copy_from_slice(&e.u32(2)); + assert!(export_exif(&counted, None).windows(5).any(|w| w == b"stale")); + } +} + #[test] fn thumbnail_removal_never_panics_on_truncated_exif() { for e in [&LE, &BE] {