Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(algo): avoid repeated copies of duplicate EXIF text (#8771)

Dylan M. Taylorecff046ea13b
diff --git a/crates/algo/src/exif.rs b/crates/algo/src/exif.rs
index d18bcdec9..15c81fbe9 100644
--- a/crates/algo/src/exif.rs
+++ b/crates/algo/src/exif.rs
@@ -87,6 +87,8 @@ impl Tiff<'_> {
}
}
fn text(&self, typ: u16, count: u32, field: usize) -> Option<String> {
+ #[cfg(test)]
+ TEXT_DECODES.with(|c| c.set(c.get() + 1));
if typ != 2 {
return None;
}
@@ -97,6 +99,9 @@ impl Tiff<'_> {
}
}
+#[cfg(test)]
+thread_local!(static TEXT_DECODES: std::cell::Cell<usize> = const { std::cell::Cell::new(0) });
+
/// Reads [`CameraInfo`] from a TIFF-structured EXIF payload (an `Exif\0\0` prefix is skipped).
pub fn read(exif: &[u8]) -> CameraInfo {
let b = exif.strip_prefix(b"Exif\0\0").unwrap_or(exif);
@@ -117,15 +122,17 @@ pub fn read(exif: &[u8]) -> CameraInfo {
seen.push(off);
for (tag, typ, count, field) in t.ifd(off) {
match tag {
- 0x010F => info.make = t.text(typ, count, field),
- 0x0110 => info.model = t.text(typ, count, field),
+ // Duplicate TIFF text tags can point at the same large payload. Keep the first
+ // recognized value and do not repeatedly decode/copy later duplicates.
+ 0x010F if info.make.is_none() => info.make = t.text(typ, count, field),
+ 0x0110 if info.model.is_none() => info.model = t.text(typ, count, field),
0x829A => info.exposure_time = t.number(typ, count, field),
0x829D => info.f_number = t.number(typ, count, field),
0x8827 => info.iso = t.number(typ, count, field),
0x920A => info.focal_length = t.number(typ, count, field),
0xA405 => info.focal_length_35mm = t.number(typ, count, field).filter(|v| *v > 0.0),
- 0xA434 => info.lens = t.text(typ, count, field),
- 0x9003 => info.date_taken = t.text(typ, count, field),
+ 0xA434 if info.lens.is_none() => info.lens = t.text(typ, count, field),
+ 0x9003 if info.date_taken.is_none() => info.date_taken = t.text(typ, count, field),
0x8769 => {
if let Some(p) = t.u32(field) {
queue.push(p as usize);
@@ -252,6 +259,47 @@ mod tests {
/// Hostile payloads: huge counts, offsets near `u32::MAX`, self-referencing IFDs and random
/// bytes read as `None` fields instead of panicking or overflowing (usize is 32 bits on wasm).
+ #[test]
+ fn duplicate_text_tags_keep_the_first_value_without_copying_large_duplicates() {
+ let value = vec![b'X'; 2 * 1024 * 1024];
+ let mut b = b"II*\0".to_vec();
+ b.extend_from_slice(&8u32.to_le_bytes());
+ // IFD0: Make="M", a duplicate Make pointing at a 2 MiB payload, and the Exif IFD link.
+ b.extend_from_slice(&3u16.to_le_bytes());
+ b.extend_from_slice(&0x010Fu16.to_le_bytes());
+ b.extend_from_slice(&2u16.to_le_bytes());
+ b.extend_from_slice(&2u32.to_le_bytes());
+ b.extend_from_slice(b"M\0\0\0");
+ b.extend_from_slice(&0x010Fu16.to_le_bytes());
+ b.extend_from_slice(&2u16.to_le_bytes());
+ b.extend_from_slice(&(value.len() as u32).to_le_bytes());
+ b.extend_from_slice(&80u32.to_le_bytes());
+ b.extend_from_slice(&0x8769u16.to_le_bytes());
+ b.extend_from_slice(&4u16.to_le_bytes());
+ b.extend_from_slice(&1u32.to_le_bytes());
+ b.extend_from_slice(&50u32.to_le_bytes());
+ b.extend_from_slice(&0u32.to_le_bytes());
+ // Exif IFD at 50: DateTimeOriginal="D" and the same large duplicate payload at offset 80.
+ b.extend_from_slice(&2u16.to_le_bytes());
+ b.extend_from_slice(&0x9003u16.to_le_bytes());
+ b.extend_from_slice(&2u16.to_le_bytes());
+ b.extend_from_slice(&2u32.to_le_bytes());
+ b.extend_from_slice(b"D\0\0\0");
+ b.extend_from_slice(&0x9003u16.to_le_bytes());
+ b.extend_from_slice(&2u16.to_le_bytes());
+ b.extend_from_slice(&(value.len() as u32).to_le_bytes());
+ b.extend_from_slice(&80u32.to_le_bytes());
+ b.extend_from_slice(&0u32.to_le_bytes());
+ b.extend_from_slice(&value);
+
+ TEXT_DECODES.with(|c| c.set(0));
+ let info = read(&b);
+ assert_eq!(info.make.as_deref(), Some("M"));
+ assert_eq!(info.date_taken.as_deref(), Some("D"));
+ // One decode per tag: duplicates must be skipped before `text` copies their payload.
+ assert_eq!(TEXT_DECODES.with(|c| c.get()), 2);
+ }
+
#[test]
fn hostile_payloads_never_panic() {
let entry = |tag: u16, typ: u16, count: u32, value: u32| {