fix(raw): bound LJ92 writer tile padding before allocation

This commit is contained in:
Dylan M. Taylor committed 2026-11-15 16:21:37 -05:00
1 parent 30596f73c5
commit da591dc6b2
1 file changed
+59 -1
+59 -1
View File
@@ -13,6 +13,7 @@ pub enum DngCompression {
Uncompressed,
/// Lossless JPEG tiles of `tile × tile` pixels (`tile` even). CFA tiles are encoded as two interleaved
/// components of half the tile width (the common DNG layout), linear data with one component per sample.
/// Edges above 2048 (after even alignment) are rejected before padding the tile.
Lj92 {
tile: u32,
},
@@ -35,6 +36,11 @@ const DEFLATE_LEVEL: u8 = 1;
/// above the 256 default and every real tile this writer makes.
const MAX_DEFLATE_TILE: u32 = 2048;
// Match the existing padded Deflate edge budget. JPEG's 16-bit dimension limit alone would permit
// a 65534²-sample allocation even for a tiny source image. At most four validated channels make
// this bound at most 32 MiB of u16 padding per tile, before encoder scratch/output.
const MAX_LJ92_TILE: usize = MAX_DEFLATE_TILE as usize;
/// `tile` aligned down to a multiple of 16 and clamped to [`MAX_DEFLATE_TILE`].
fn deflate_tile(tile: u32) -> usize {
(tile.max(16) & !15).min(MAX_DEFLATE_TILE) as usize
@@ -270,7 +276,7 @@ pub fn write_dng(raw: &RawImage, opts: &DngWriteOptions) -> Result<Vec<u8>> {
}
DngCompression::Lj92 { tile } => {
let tile = (tile.max(2) & !1) as usize;
if tile > 65534 {
if tile > MAX_LJ92_TILE {
return Err(RawError::Limit("tile too large"));
}
ifd.set(t::COMPRESSION, Value::Short(vec![compression::JPEG]));
@@ -432,6 +438,58 @@ mod tests {
use super::*;
use crate::{BlackLevel, OpcodeLists, RawData, RawFormat, RawImage, Rect};
fn tiny_raw() -> RawImage {
let (w, h) = (8usize, 8usize);
let full = Rect::new(0, 0, w, h);
RawImage {
format: RawFormat::Dng,
width: w,
height: h,
cpp: 1,
data: RawData::U16((0..64).map(|i| i as u16 * 129).collect()),
cfa: None,
bits: 14,
black: BlackLevel::uniform(0.0),
white: vec![16383.0],
active_area: full,
crop: full,
orientation: crate::Orientation::Normal,
color: crate::ColorData::default(),
wb_multipliers: None,
linearized: false,
pixel_aspect: 1.0,
opcodes: OpcodeLists::default(),
camera: Default::default(),
cinema: Default::default(),
lens_metadata: Default::default(),
level_sources: Default::default(),
notices: crate::RawNotices::default(),
}
}
#[test]
fn oversized_lj92_padding_is_rejected() {
let raw = tiny_raw();
// The near-boundary case previously completed with an oversized padded tile; keep it first
// so reverting the guard fails without trying the multi-gigabyte cases.
for tile in [2050, 65534, u32::MAX] {
let opts = DngWriteOptions { compression: DngCompression::Lj92 { tile }, ..Default::default() };
assert!(matches!(write_dng(&raw, &opts), Err(RawError::Limit("tile too large"))));
}
}
#[test]
fn default_lj92_tiles_preserve_samples() {
let raw = tiny_raw();
let bytes = write_dng(&raw, &DngWriteOptions::default()).expect("normal tiles write");
let back = crate::decode(&bytes).expect("normal tiles decode");
assert_eq!((back.width, back.height), (raw.width, raw.height));
match (&back.data, &raw.data) {
(RawData::U16(actual), RawData::U16(expected)) => assert_eq!(actual, expected),
_ => panic!("integer DNG changed sample type"),
}
}
/// Deflate tiles of `tile × tile` pad into a square buffer that grows with the square: a
/// caller-supplied edge of `u32::MAX` would make the writer pad an 8 × 8 image into billions of
/// edge-replicate iterations and a multi-gigabyte buffer. The clamp keeps the bounded shape — same