Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Merge PR #1535

Brandon Thomasb18ce72b610d
diff --cc crates/raw/src/vendor/pef.rs
index a52154705,4020829e4..86db33540
--- a/crates/raw/src/vendor/pef.rs
+++ b/crates/raw/src/vendor/pef.rs
@@@ -392,21 -394,34 +396,49 @@@ mod tests
assert!(table(&tb[..20], ByteOrder::Big).is_err());
}
+ /// Zero-padded lookahead past the end must not be decoded as samples: with no entropy at all, the zero bits
+ /// are a valid code (category 1) in this table, so a reader that only limits the refill position accepts them.
+ #[test]
+ fn symbols_decoded_from_padding_past_the_end_are_rejected() {
+ let (tb, codes) = test_table();
+ let h = table(&tb, ByteOrder::Big).unwrap();
+ assert!(decode_huffman(&[], &h, 2, 1, 14).is_err());
+ // the same image with its real entropy still decodes, including the last byte's padding bits
+ let px = [5u16, 9];
+ let src = encode(&px, 2, &codes);
+ assert_eq!(decode_huffman(&src, &h, 2, 1, 14).unwrap(), px);
+ // one byte short of the real entropy is rejected
+ assert!(decode_huffman(&src[..src.len() - 1], &h, 2, 1, 14).is_err());
+ }
+
+ /// A long run of the largest difference drove a predictor past i32: a panic in builds with overflow checks,
+ /// wrapped values otherwise (issue #1515). Both the row and the column predictors now stop with an error.
+ #[test]
+ fn predictor_overflow_is_an_error_not_a_panic() {
+ let (tb, codes) = test_table();
+ let h = table(&tb, ByteOrder::Big).unwrap();
+ let (c, l) = codes[14];
+ let d = (1u32 << 14) - 1;
+ let stream = |n: usize| {
+ let mut bits: Vec<bool> = Vec::with_capacity(n * (l as usize + 14));
+ for _ in 0..n {
+ (0..l).rev().for_each(|i| bits.push((c >> (12 - l)) >> i & 1 == 1));
+ (0..14).rev().for_each(|i| bits.push(d >> i & 1 == 1));
+ }
+ bits.chunks(8).map(|c| c.iter().enumerate().fold(0u8, |a, (i, &b)| a | (b as u8) << (7 - i))).collect::<Vec<u8>>()
+ };
+ // differences one predictor needs to pass i32::MAX
+ let steps = (i32::MAX as u32 / d) as usize + 2;
+ // along a row: every other sample adds to the same predictor
+ let w = 2 * steps + 2;
+ let err = decode_huffman(&stream(w), &h, w, 1, 14).unwrap_err();
+ assert!(matches!(err, RawError::Corrupt(ref m) if m.contains("predictor")), "{err}");
+ // down two columns: each row adds to the predictor of its parity
+ let rows = 2 * steps + 2;
+ let err = decode_huffman(&stream(2 * rows), &h, 2, rows, 14).unwrap_err();
+ assert!(matches!(err, RawError::Corrupt(ref m) if m.contains("predictor")), "{err}");
+ }
+
/// A big-endian one-strip PEF-style TIFF of `w × h` 12-bit samples with the given compression tag and strip
/// byte count, around `payload`.
fn tiny_pef(w: u16, h: u16, compression: u16, strip_len: u32, payload: &[u8]) -> Vec<u8> {