From 006e79b5ab10a9f8d305feadedad02fa89644ab1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Thu, 19 Nov 2026 23:22:57 -0500 Subject: [PATCH] Restore JPEG metadata preflight parity (#7930) --- crates/create/src/upright_image.rs | 32 +++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/crates/create/src/upright_image.rs b/crates/create/src/upright_image.rs index 9efaad9fd2..352fbd1682 100644 --- a/crates/create/src/upright_image.rs +++ b/crates/create/src/upright_image.rs @@ -113,12 +113,34 @@ pub(crate) fn without_extended_xmp(bytes: &[u8]) -> Vec { /// Calls `f` with the byte range of each Extended XMP chunk (APP1 segment) before the image data. fn each_extended_xmp(bytes: &[u8], mut f: impl FnMut(std::ops::Range)) { const PREFIX: &[u8] = b"http://ns.adobe.com/xmp/extension/\0"; - let _ = crate::image_probe::walk_jpeg_segments(bytes, |marker, span, seg| { - if marker == 0xE1 && seg.starts_with(PREFIX) { - f(span); + let mut i = 2usize; + while let Some(&byte) = bytes.get(i) { + // Stray bytes between segments, which the decoder skips too. + if byte != 0xFF { + i += 1; + continue; } - Ok(crate::image_probe::JpegVisit::Continue) - }); + let Some(&marker) = bytes.get(i.saturating_add(1)) else { break }; + match marker { + // Fill bytes before a marker. + 0xFF => i += 1, + // Markers without a segment. + 0x00 | 0x01 | 0xD0..=0xD7 => i += 2, + // The image data starts (or ends): no headers after it. + 0xDA | 0xD9 => break, + _ => { + let Some(&[hi, lo]) = bytes.get(i.saturating_add(2)..i.saturating_add(4)) else { break }; + let len = usize::from(u16::from_be_bytes([hi, lo])); + let end = i.saturating_add(2).saturating_add(len.max(2)); + // The prefix must start the APP1 segment's own payload: a zero-length header + // followed by prefix bytes is not a chunk. + if marker == 0xE1 && bytes.get(i.saturating_add(4)..end.min(bytes.len())).is_some_and(|seg| seg.starts_with(PREFIX)) { + f(i..end.min(bytes.len())); + } + i = end; + } + } + } } fn jfif_density_dpi(bytes: &[u8]) -> Option<(f32, f32)> {