Skip to content

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

RAW: check the orientation range before narrowing (#1839)

kayrockscreenprinting86a629939a14
diff --git a/crates/raw/src/cr2.rs b/crates/raw/src/cr2.rs
index a0fe896c8..5b3601222 100644
--- a/crates/raw/src/cr2.rs
+++ b/crates/raw/src/cr2.rs
@@ -289,7 +289,7 @@ pub(crate) fn decode(t: &Tiff, limits: &Limits) -> Result<Sensor> {
crop: active,
color: Default::default(),
camera_wb,
- orientation: t.tag_uint(&ifd0, tag::ORIENTATION).map(|o| o as u16).filter(|o| (1..=8).contains(o)).unwrap_or(1),
+ orientation: crate::tiff::orientation(t.tag_uint(&ifd0, tag::ORIENTATION)),
baseline_exposure: 0.0,
gain_maps: Vec::new(),
tone_curve: Vec::new(),
diff --git a/crates/raw/src/dng.rs b/crates/raw/src/dng.rs
index 96931227a..0f2344ce3 100644
--- a/crates/raw/src/dng.rs
+++ b/crates/raw/src/dng.rs
@@ -258,7 +258,7 @@ pub(crate) fn decode(t: &Tiff, limits: &Limits) -> Result<Sensor> {
crop,
color,
camera_wb: None,
- orientation: t.tag_uint(&ifd0, tag::ORIENTATION).map(|o| o as u16).filter(|o| (1..=8).contains(o)).unwrap_or(1),
+ orientation: crate::tiff::orientation(t.tag_uint(&ifd0, tag::ORIENTATION)),
baseline_exposure: if baseline_exposure.is_finite() { baseline_exposure.clamp(-10.0, 10.0) } else { 0.0 },
gain_maps,
tone_curve: Vec::new(),
diff --git a/crates/raw/src/orf.rs b/crates/raw/src/orf.rs
index cc5236235..ba1592672 100644
--- a/crates/raw/src/orf.rs
+++ b/crates/raw/src/orf.rs
@@ -146,7 +146,7 @@ pub(crate) fn decode(t: &Tiff, limits: &Limits) -> Result<Sensor> {
crop,
color: Default::default(),
camera_wb,
- orientation: t.tag_uint(&raw, tag::ORIENTATION).map(|o| o as u16).filter(|o| (1..=8).contains(o)).unwrap_or(1),
+ orientation: crate::tiff::orientation(t.tag_uint(&raw, tag::ORIENTATION)),
baseline_exposure: 0.0,
gain_maps: Vec::new(),
tone_curve: Vec::new(),
diff --git a/crates/raw/src/rw2.rs b/crates/raw/src/rw2.rs
index 805baeb39..fe1e0b2ab 100644
--- a/crates/raw/src/rw2.rs
+++ b/crates/raw/src/rw2.rs
@@ -166,7 +166,7 @@ pub(crate) fn decode(t: &Tiff, limits: &Limits) -> Result<Sensor> {
crop,
color: Default::default(),
camera_wb,
- orientation: t.tag_uint(&ifd, tag::ORIENTATION).map(|o| o as u16).filter(|o| (1..=8).contains(o)).unwrap_or(1),
+ orientation: crate::tiff::orientation(t.tag_uint(&ifd, tag::ORIENTATION)),
baseline_exposure: 0.0,
gain_maps: Vec::new(),
tone_curve: Vec::new(),
diff --git a/crates/raw/src/tiff.rs b/crates/raw/src/tiff.rs
index 5a623044b..6313f52e8 100644
--- a/crates/raw/src/tiff.rs
+++ b/crates/raw/src/tiff.rs
@@ -313,6 +313,12 @@ impl<'a> Tiff<'a> {
}
}
+/// The EXIF orientation (1..=8) of a raw tag value; anything else is the default 1. The range is
+/// checked on the `u32`: narrowing first let a LONG like 65538 pass as 2 (#1817).
+pub(crate) fn orientation(v: Option<u32>) -> u16 {
+ v.filter(|o| (1..=8).contains(o)).and_then(|o| u16::try_from(o).ok()).unwrap_or(1)
+}
+
/// A human-readable listing of every IFD and entry (debugging aid).
pub(crate) fn dump(data: &[u8]) -> String {
use std::fmt::Write;
@@ -338,6 +344,17 @@ pub(crate) fn dump(data: &[u8]) -> String {
mod tests {
use super::*;
+ #[test]
+ fn orientation_is_range_checked_before_narrowing() {
+ // #1817: 65538 as u16 is 2, which passed the 1..=8 check.
+ for (v, want) in [(None, 1), (Some(0), 1), (Some(9), 1), (Some(65_538), 1), (Some(65_537), 1), (Some(u32::MAX), 1)] {
+ assert_eq!(orientation(v), want, "{v:?}");
+ }
+ for o in 1..=8u32 {
+ assert_eq!(orientation(Some(o)), o as u16);
+ }
+ }
+
#[test]
fn header_variants() {
assert!(Tiff::new(b"II*\0\x08\0\0\0").is_some());
diff --git a/crates/raw/src/tiffep.rs b/crates/raw/src/tiffep.rs
index 0f014ea19..8fd47836b 100644
--- a/crates/raw/src/tiffep.rs
+++ b/crates/raw/src/tiffep.rs
@@ -209,7 +209,7 @@ pub(crate) fn decode(t: &Tiff, format: RawFormat, limits: &Limits) -> Result<Sen
crop,
color: Default::default(),
camera_wb,
- orientation: ifd0.and_then(|i| t.tag_uint(&i, tag::ORIENTATION)).map(|o| o as u16).filter(|o| (1..=8).contains(o)).unwrap_or(1),
+ orientation: crate::tiff::orientation(ifd0.and_then(|i| t.tag_uint(&i, tag::ORIENTATION))),
baseline_exposure: 0.0,
gain_maps: Vec::new(),
tone_curve: Vec::new(),
diff --git a/crates/raw/tests/dng.rs b/crates/raw/tests/dng.rs
index 32224b54f..eaf6e2e2a 100644
--- a/crates/raw/tests/dng.rs
+++ b/crates/raw/tests/dng.rs
@@ -71,6 +71,27 @@ fn linear_raw_decodes_and_develops() {
assert!(d.info.cfa.is_none());
}
+/// `spec`'s file with its Orientation entry (a SHORT `from`) rewritten as a LONG `to`.
+fn with_long_orientation(spec: &DngSpec, from: u16, to: u32) -> Vec<u8> {
+ let mut b = spec.build();
+ let short = [&[0x12, 0x01, 3, 0, 1, 0, 0, 0][..], &from.to_le_bytes(), &[0, 0]].concat();
+ let at: Vec<usize> = b.windows(short.len()).enumerate().filter(|(_, w)| *w == short.as_slice()).map(|(i, _)| i).collect();
+ assert_eq!(at.len(), 1, "one Orientation entry");
+ let long = [&[0x12, 0x01, 4, 0, 1, 0, 0, 0][..], &to.to_le_bytes()].concat();
+ b[at[0]..at[0] + long.len()].copy_from_slice(&long);
+ b
+}
+
+#[test]
+fn an_orientation_past_u16_is_not_truncated_into_range() {
+ // #1817: 65538 truncated to 2 and rotated the image; it is out of range, so 1.
+ let mut spec = DngSpec::cfa(8, 6, vec![500; 48]);
+ spec.orientation = 2;
+ assert_eq!(sensor(&with_long_orientation(&spec, 2, 65_538)).orientation, 1);
+ // Control: a LONG orientation in range is read.
+ assert_eq!(sensor(&with_long_orientation(&spec, 2, 6)).orientation, 6);
+}
+
#[test]
fn metadata_is_read() {
let (w, h) = (40, 30);