Merge remote-tracking branch 'upstream/main' into pr-5332

This commit is contained in:
FredApps committed 2026-11-03 12:21:30 +01:00
commit 362baf5faa
12 files changed
+362 -57

No files matched your search

+22 -5
View File
@@ -491,14 +491,31 @@ pub fn portable_marker(exe_dir: &Path) -> Option<PathBuf> {
PORTABLE_MARKERS.iter().map(|m| exe_dir.join(m)).find(|p| p.is_file())
}
/// Probe names tried before giving up on a portable data folder.
const WRITE_PROBE_ATTEMPTS: u32 = 64;
/// Create `dir` and prove a file can be written in it (a folder can exist yet be read-only).
///
/// The probe is created exclusively (`create_new`), so an entry already at its name (a link
/// someone else placed there, say) is never opened, written through or removed: the next name
/// is tried instead. Only a probe this call created is removed.
fn ensure_writable(dir: &Path) -> std::io::Result<()> {
std::fs::create_dir_all(dir)?;
let probe = dir.join(format!(".write-test-{}", std::process::id()));
std::fs::write(&probe, b"")?;
// A leftover probe is harmless; don't fail portable mode over it.
let _ = std::fs::remove_file(&probe);
Ok(())
let pid = std::process::id();
for attempt in 0..WRITE_PROBE_ATTEMPTS {
let probe = dir.join(format!(".write-test-{pid}-{attempt}"));
match std::fs::OpenOptions::new().write(true).create_new(true).open(&probe) {
Ok(file) => {
drop(file);
// A leftover probe is harmless; don't fail portable mode over it.
let _ = std::fs::remove_file(&probe);
return Ok(());
}
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue,
Err(e) => return Err(e),
}
}
Err(std::io::Error::new(std::io::ErrorKind::AlreadyExists, "every write-test name is already taken"))
}
#[cfg(test)]
+25
View File
@@ -443,3 +443,28 @@ fn missing_exe_dir_or_env_never_panics() {
let d = resolve(env_of(&[("PHOTOCRAFT_CONFIG_DIR", &empty), ("HOME", &empty), ("APPDATA", &empty)]), None);
assert_eq!(d.dir, None, "empty variables are ignored");
}
#[cfg(unix)]
#[test]
fn the_write_probe_never_follows_an_entry_already_at_its_name() {
let root = temp("probe-link");
let data = root.join("data");
std::fs::create_dir_all(&data).unwrap();
let victim = root.join("victim.txt");
std::fs::write(&victim, b"keep me").unwrap();
// Every name the probe tries, except the last, is a link to a file outside the folder.
let pid = std::process::id();
for attempt in 0..WRITE_PROBE_ATTEMPTS - 1 {
std::os::unix::fs::symlink(&victim, data.join(format!(".write-test-{pid}-{attempt}"))).unwrap();
}
ensure_writable(&data).unwrap();
assert_eq!(std::fs::read(&victim).unwrap(), b"keep me");
// The links are left alone, and the probe that was created is gone.
assert!(std::fs::symlink_metadata(data.join(format!(".write-test-{pid}-0"))).unwrap().file_type().is_symlink());
assert!(!data.join(format!(".write-test-{pid}-{}", WRITE_PROBE_ATTEMPTS - 1)).exists());
// With every name taken the folder is reported unwritable, still without touching the file.
std::os::unix::fs::symlink(&victim, data.join(format!(".write-test-{pid}-{}", WRITE_PROBE_ATTEMPTS - 1))).unwrap();
assert!(ensure_writable(&data).is_err());
assert_eq!(std::fs::read(&victim).unwrap(), b"keep me");
let _ = std::fs::remove_dir_all(&root);
}
+9
View File
@@ -20,6 +20,11 @@ const MAX_ENTRIES: usize = 1 << 22;
/// Bytes of entry names the allocation tables may name. A name is retained twice (in the entry
/// and as its map key), and tables can point many names at the same bytes.
const MAX_NAME_BYTES: usize = 32 << 20;
/// File and directory records parsed across every allocation table. Each table walks its own
/// records, and tables may start inside one another and share a long suffix, so the per-table
/// counts do not bound the work: a few thousand overlapping tables could reparse one large run
/// of records each. Real files hold a few hundred saves of a few thousand entries.
const MAX_TABLE_RECORDS: usize = 1 << 25;
/// zstd window ceiling: real documents use a few MiB; this stops a header from reserving gigabytes.
const MAX_ZSTD_WINDOW: u64 = 64 << 20;
@@ -175,6 +180,7 @@ impl<'a> Archive<'a> {
let mut seen = HashSet::new();
let mut next = header.fat_offset;
let mut tables = 0usize;
let mut records_left = MAX_TABLE_RECORDS;
while next != 0 {
if !seen.insert(next) {
return Err(Error::Malformed("allocation tables form a cycle"));
@@ -199,6 +205,9 @@ impl<'a> Archive<'a> {
c.take(8)?;
let dirs = c.u16()?;
c.take(1)?;
// Charge the declared records before walking them.
let declared = usize::try_from(files).unwrap_or(usize::MAX).saturating_add(usize::from(dirs));
records_left = records_left.checked_sub(declared).ok_or(Error::Limit("too many allocation records"))?;
for _ in 0..files {
let id = c.u32()?;
let flag = c.u8()?;
+3 -1
View File
@@ -177,7 +177,9 @@ pub(crate) fn curves(r: &mut Reader, crvs: ObjId) -> Option<Path> {
r.warn("curves with more than four million nodes");
return None;
}
if let Err(reason) = r.geometry_budget.records(len) {
// Charge the subpath itself as well as its records: an empty subpath adds no records
// or points, but a shared curve full of them is still walked for every occurrence.
if let Err(reason) = r.geometry_budget.records(len.saturating_add(1)) {
r.warn(reason);
return None;
}
+86 -27
View File
@@ -125,18 +125,24 @@ pub struct Stroke {
/// Gradient stops per gradient.
const MAX_STOPS: usize = 1024;
/// Dash and gap lengths per stroke. Affinity's editor offers three dash/gap pairs; a longer
/// pattern would be copied into every shape that shares its style.
const MAX_DASHES: usize = 64;
fn floats(v: &Value) -> Option<Vec<f64>> {
/// The first `n` components of a float vector or packed `f32` record. Colours and gradient
/// positions use a few leading components; a shared colour with a huge record is decoded for
/// every occurrence, so the rest is neither converted nor copied.
fn floats(v: &Value, n: usize) -> Option<Vec<f64>> {
match v {
Value::Floats(f) => Some(f.clone()),
Value::Bytes(b) if b.len() % 4 == 0 => Some(f32_samples(b)),
Value::Floats(f) => Some(f.iter().take(n).copied().collect()),
Value::Bytes(b) if b.len() % 4 == 0 => Some(f32_samples(b, n)),
_ => None,
}
}
/// Little-endian `f32` samples widened to `f64`.
fn f32_samples(b: &[u8]) -> Vec<f64> {
b.as_chunks::<4>().0.iter().map(|c| f64::from(f32::from_le_bytes(*c))).collect()
/// Up to `n` little-endian `f32` samples widened to `f64`.
fn f32_samples(b: &[u8], n: usize) -> Vec<f64> {
b.as_chunks::<4>().0.iter().take(n).map(|c| f64::from(f32::from_le_bytes(*c))).collect()
}
/// Spot colours defined through spot colours. A spot colour (`Pant`) is read through its RGB
@@ -154,26 +160,27 @@ pub(crate) fn color(r: &mut Reader, id: ObjId) -> Option<Color> {
fn color_at(r: &mut Reader, id: ObjId, depth: usize) -> Option<Color> {
let s = r.s;
let class = s.class(id)?;
let col = || -> Option<Vec<f64>> {
let v = floats(s.field(id, b"_col")?)?;
// `n` is how many components the model reads; only those are decoded and checked.
let col = |n: usize| -> Option<Vec<f64>> {
let v = floats(s.field(id, b"_col")?, n)?;
v.iter().all(|f| f.is_finite()).then_some(v)
};
Some(match &class.0.to_be_bytes() {
b"RGBA" => {
let v = col()?;
let v = col(4)?;
Color::Rgb { r: *v.first()?, g: *v.get(1)?, b: *v.get(2)?, a: v.get(3)?.clamp(0.0, 1.0) }
}
b"HSLA" => {
let v = col()?;
let v = col(4)?;
let (r2, g, b) = hsl(*v.first()?, *v.get(1)?, *v.get(2)?);
Color::Rgb { r: r2, g, b, a: v.get(3)?.clamp(0.0, 1.0) }
}
b"CMYK" => {
let v = col()?;
let v = col(5)?;
Color::Cmyk { c: *v.first()?, m: *v.get(1)?, y: *v.get(2)?, k: *v.get(3)?, a: v.get(4)?.clamp(0.0, 1.0) }
}
b"GRAY" => {
let v = col()?;
let v = col(2)?;
Color::Gray { v: *v.first()?, a: v.get(1)?.clamp(0.0, 1.0) }
}
b"LABA" => {
@@ -245,9 +252,10 @@ pub(crate) fn descriptor(r: &mut Reader, id: ObjId, local: Affine) -> Option<Pai
}
let cols = s.objs(grad, b"Cols");
let posn: Vec<Vec<f64>> = match s.field(grad, b"Posn") {
Some(Value::Array(a)) => a.iter().filter_map(floats).take(MAX_STOPS).collect(),
// A position is (offset, bias): later components are not decoded.
Some(Value::Array(a)) => a.iter().filter_map(|v| floats(v, 2)).take(MAX_STOPS).collect(),
Some(Value::Records { size, data }) if *size % 4 == 0 && *size > 0 => {
data.chunks_exact(usize::from(*size)).map(f32_samples).take(MAX_STOPS).collect()
data.chunks_exact(usize::from(*size)).map(|b| f32_samples(b, 2)).take(MAX_STOPS).collect()
}
_ => Vec::new(),
};
@@ -274,23 +282,52 @@ pub(crate) fn descriptor(r: &mut Reader, id: ObjId, local: Affine) -> Option<Pai
}
}
fn current(r: &Reader, id: ObjId, list: &[u8; 4], index: &[u8; 4]) -> Option<ObjId> {
let items = r.s.objs(id, list);
let i = r.s.int(id, index).and_then(|i| usize::try_from(i).ok()).unwrap_or(0);
items.get(i).or_else(|| items.first()).copied()
/// The active entry of a paint list (the `index`-th object, else the first) and how many
/// objects the list holds. Every raw slot, null ones included, is charged before the list is
/// walked: a shared layer's lists are read again for each occurrence. A list beyond the
/// document's work budget reads as empty.
fn current(r: &mut Reader, id: ObjId, list: &[u8; 4], index: &[u8; 4]) -> (Option<ObjId>, usize) {
let s = r.s;
let Some(Value::Array(items)) = s.field(id, list) else { return (None, 0) };
if r.charge_slots(items.len()).is_err() {
r.warn("fill and stroke lists beyond the document work budget (left empty)");
return (None, 0);
}
let want = s.int(id, index).and_then(|i| usize::try_from(i).ok()).unwrap_or(0);
let (mut first, mut chosen, mut count) = (None, None, 0usize);
for o in items.iter().filter_map(|v| if let Value::Obj(Some(o)) = v { Some(*o) } else { None }) {
first = first.or(Some(o));
if count == want {
chosen = Some(o);
}
count += 1;
}
(chosen.or(first), count)
}
/// The fills of `list` (artboard backgrounds use `BFFl`).
pub(crate) fn fills(r: &mut Reader, id: ObjId, local: Affine, list: &[u8; 4]) -> Vec<crate::paint::Paint> {
let d = current(r, id, list, b"BFCr").or_else(|| r.s.obj(id, b"BFil"));
d.and_then(|d| descriptor(r, d, local)).filter(|p| *p != Paint::None).into_iter().collect()
fills_counted(r, id, local, list).0
}
/// [`fills`], and how many entries `list` holds.
fn fills_counted(r: &mut Reader, id: ObjId, local: Affine, list: &[u8; 4]) -> (Vec<Paint>, usize) {
let (d, count) = current(r, id, list, b"BFCr");
let d = d.or_else(|| r.s.obj(id, b"BFil"));
(d.and_then(|d| descriptor(r, d, local)).filter(|p| *p != Paint::None).into_iter().collect(), count)
}
/// A node's fill and stroke. `local` maps the node's space to document pixels.
pub(crate) fn node_paint(r: &mut Reader, id: ObjId, local: Affine) -> (Vec<Paint>, Vec<Stroke>) {
let s = r.s;
let fills = fills(r, id, local, b"BFFl");
if r.s.objs(id, b"BFFl").len() > 1 || r.s.objs(id, b"LIFl").len() > 1 {
let (fills, fill_count) = fills_counted(r, id, local, b"BFFl");
let (pen, style, pen_count) = if s.field(id, b"LIFl").is_some() {
let (pen, pens) = current(r, id, b"LIFl", b"LICr");
(pen, current(r, id, b"LILn", b"LICr").0, pens)
} else {
(s.obj(id, b"PFil"), s.obj(id, b"LSty"), 0)
};
if fill_count > 1 || pen_count > 1 {
r.warn("several fills or strokes on one object (only the active one is imported)");
}
if s.f64(id, b"FOpc").is_some_and(|o| o < 0.999) {
@@ -299,11 +336,6 @@ pub(crate) fn node_paint(r: &mut Reader, id: ObjId, local: Affine) -> (Vec<Paint
if s.obj(id, b"Trns").is_some() {
r.warn("transparency gradients (imported without them)");
}
let (pen, style) = if s.field(id, b"LIFl").is_some() {
(current(r, id, b"LIFl", b"LICr"), current(r, id, b"LILn", b"LICr"))
} else {
(s.obj(id, b"PFil"), s.obj(id, b"LSty"))
};
let behind = matches!(s.field(id, b"DrwO"), Some(Value::Array(a)) if a.first() == Some(&Value::Enum { id: 1, version: 0 }));
let stroke = match (pen, style) {
(Some(pen), Some(style)) => stroke(r, pen, style, local, behind),
@@ -341,6 +373,17 @@ fn stroke(r: &mut Reader, pen: ObjId, ldsc: ObjId, local: Affine, behind: bool)
_ => Align::Center,
};
let dash = match s.field(lsty, b"Patn") {
// Each occurrence of a shared style keeps its own scaled copy: refuse long patterns
// before scanning or copying them.
Some(Value::Array(a)) if a.len() > MAX_DASHES => {
r.warn("dash patterns longer than 64 entries (imported as solid strokes)");
None
}
// The copies are retained across the document: charge them like other shared slots.
Some(Value::Array(a)) if r.charge_slots(a.len()).is_err() => {
r.warn("dash patterns beyond the document work budget (imported as solid strokes)");
None
}
Some(Value::Array(a)) if data.get(10) == Some(&2) || !a.is_empty() => {
let mut v: Vec<f64> = a.iter().filter_map(|x| if let Value::Float(f) = x { Some(*f) } else { None }).collect();
while v.len() >= 2 && v.iter().rev().take(2).all(|x| *x == 0.0) {
@@ -356,3 +399,19 @@ fn stroke(r: &mut Reader, pen: ObjId, ldsc: ObjId, local: Affine, behind: bool)
}
Some(Stroke { paint, width, cap, join, miter_limit, align, dash, behind })
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn only_the_components_a_schema_reads_are_decoded() {
// A 64 KiB record decodes to the four components asked for, not sixteen thousand.
let record = Value::Bytes(1.0f32.to_le_bytes().repeat(16_383));
assert_eq!(floats(&record, 4), Some(vec![1.0; 4]));
assert_eq!(floats(&Value::Floats(vec![0.5; 6]), 2), Some(vec![0.5; 2]));
// Fewer components than asked for are returned as they are, for the caller to refuse.
assert_eq!(floats(&Value::Floats(vec![0.5; 3]), 5), Some(vec![0.5; 3]));
assert_eq!(floats(&Value::Bytes(vec![0; 3]), 4), None);
}
}
+13 -6
View File
@@ -187,12 +187,12 @@ fn plane(
Some(Value::Array(a)) => a,
_ => &[],
};
let mut tiles = match s.field(bitmap, &tag(b"Idx", c)) {
Some(Value::Array(a)) => a.as_slice(),
// Tile references in order. Null slots are skipped, but every slot walked (null or not) is
// charged below, so a shared bitmap's long null prefix is not rescanned for free.
let mut tiles: &[Value] = match s.field(bitmap, &tag(b"Idx", c)) {
Some(Value::Array(a)) => a,
_ => &[],
}
.iter()
.filter_map(|v| if let Value::Obj(Some(o)) = v { Some(*o) } else { None });
};
// The scan below runs for every occurrence of a shared bitmap, whatever it produces.
r.charge_slots(states.len().min(tw.saturating_mul(th)))?;
let row = (w as usize) * bps;
@@ -206,7 +206,14 @@ fn plane(
0 | 1 => None,
2 => Some(0xFF),
4 => {
let Some(blck) = tiles.next() else { return Err(Error::Malformed("missing pixel tile")) };
let Some(at) = tiles.iter().position(|v| matches!(v, Value::Obj(Some(_)))) else {
r.charge_slots(tiles.len())?;
return Err(Error::Malformed("missing pixel tile"));
};
r.charge_slots(at + 1)?;
let Some(Value::Obj(Some(blck))) = tiles.get(at) else { return Err(Error::Malformed("missing pixel tile")) };
let blck = *blck;
tiles = tiles.get(at + 1..).unwrap_or_default();
let (ox, oy) = (tx * TILE, ty * TILE);
if ox >= bx1 || ox + TILE <= bx0 || oy >= py1 || oy + TILE <= py0 {
continue;
+9 -6
View File
@@ -12,9 +12,10 @@ const KAPPA: f64 = 0.552_284_749_830_793_4;
const MAX_SIDES: i64 = 10_000;
/// The last value of a tag: a shape changed from one kind to another keeps a stale first block.
/// Indexed: a shared shape is read again for every occurrence, and rescanning a large field
/// list each time multiplied into work far beyond the file's size.
fn last<'a>(r: &'a Reader, id: ObjId, tag: &[u8; 4]) -> Option<&'a Value> {
let t = Tag::of(tag);
r.s.object(id)?.fields.iter().rev().find(|(k, _)| *k == t).map(|(_, v)| v)
r.s.object(id)?.get_last(Tag::of(tag))
}
fn num(r: &Reader, id: ObjId, tag: &[u8; 4]) -> Option<f64> {
@@ -51,12 +52,14 @@ pub(crate) fn shape(r: &mut Reader, shape: ObjId, b: [f64; 4], world: Affine) ->
let class = r.s.class(shape)?;
let sub = match &class.0.to_be_bytes() {
b"ShNR" | b"ShRR" => {
let types = match last(r, shape, b"CTyp") {
Some(Value::Array(a)) => a.iter().map(|t| if let Value::Enum { id, .. } = t { Some(*id) } else { None }).collect(),
// Only the four corners are read: a longer array is not scanned or copied, which
// would otherwise repeat for every occurrence of a shared shape.
let types: Vec<Option<u16>> = match last(r, shape, b"CTyp") {
Some(Value::Array(a)) => a.iter().take(4).map(|t| if let Value::Enum { id, .. } = t { Some(*id) } else { None }).collect(),
_ => Vec::new(),
};
let radii = match last(r, shape, b"ShCR") {
Some(Value::Floats(v)) => v.iter().map(|f| if f.is_finite() && *f > 0.0 { *f } else { 0.0 }).collect(),
let radii: Vec<f64> = match last(r, shape, b"ShCR") {
Some(Value::Floats(v)) => v.iter().take(4).map(|f| if f.is_finite() && *f > 0.0 { *f } else { 0.0 }).collect(),
_ => Vec::new(),
};
// Corners follow the first one (top left) unless they were unlocked; then each has
+34 -12
View File
@@ -90,26 +90,34 @@ pub struct Object {
pub chain: Vec<Tag>,
/// Fields in stream order; positional fields have tag 0.
pub fields: Vec<(Tag, Value)>,
/// Index for objects with more than [`SCAN`] fields: (tag, index of its first occurrence)
/// sorted by tag, so `get` is a binary search. A shared object can be referenced by many
/// sibling layers, and each reference used to rescan all of its fields; indexing once keeps
/// repeated lookups from multiplying into quadratic work. Smaller objects have no index
/// (and no per-object hash map to build): scanning a handful of fields is cheaper.
index: Vec<(Tag, u32)>,
/// Index for objects with more than [`SCAN`] fields: (tag, index of its first occurrence,
/// index of its last occurrence) sorted by tag, so `get` and `get_last` are a binary search.
/// A shared object can be referenced by many sibling layers, and each reference used to
/// rescan all of its fields; indexing once keeps repeated lookups from multiplying into
/// quadratic work. Smaller objects have no index (and no per-object hash map to build):
/// scanning a handful of fields is cheaper.
index: Vec<(Tag, u32, u32)>,
}
/// Objects with at most this many fields are scanned instead of indexed.
const SCAN: usize = 12;
fn build_index(fields: &[(Tag, Value)]) -> Vec<(Tag, u32)> {
fn build_index(fields: &[(Tag, Value)]) -> Vec<(Tag, u32, u32)> {
if fields.len() <= SCAN {
return Vec::new();
}
// Positional fields share tag 0 and are not looked up by tag. Sorting by (tag, index) and
// keeping the first of each tag preserves first-match semantics.
let mut index: Vec<(Tag, u32)> = fields.iter().zip(0u32..).filter(|((t, _), _)| t.0 != 0).map(|((t, _), i)| (*t, i)).collect();
// Positional fields share tag 0 and are not looked up by tag. Sorting by (tag, index) puts
// each tag's occurrences together in stream order: the first and last of each run keep
// first-match semantics for `get` and last-match semantics for `get_last`.
let mut index: Vec<(Tag, u32, u32)> = fields.iter().zip(0u32..).filter(|((t, _), _)| t.0 != 0).map(|((t, _), i)| (*t, i, i)).collect();
index.sort_unstable();
index.dedup_by_key(|e| e.0);
index.dedup_by(|later, kept| {
let same = later.0 == kept.0;
if same {
kept.2 = later.2;
}
same
});
index
}
@@ -127,7 +135,21 @@ impl Object {
return self.fields.iter().find(|(t, _)| *t == tag).map(|(_, v)| v);
}
let at = self.index.binary_search_by_key(&tag, |e| e.0).ok()?;
self.index.get(at).and_then(|&(_, i)| self.fields.get(i as usize)).map(|(_, v)| v)
self.index.get(at).and_then(|&(_, i, _)| self.fields.get(i as usize)).map(|(_, v)| v)
}
/// The last field with `tag`, for objects that may repeat a tag (a shape changed from one
/// kind to another keeps a stale first block). Indexed like [`Object::get`], so repeated
/// lookups on a large shared object do not rescan its fields.
pub fn get_last(&self, tag: Tag) -> Option<&Value> {
if tag.0 == 0 {
return None;
}
if self.fields.len() <= SCAN {
return self.fields.iter().rev().find(|(t, _)| *t == tag).map(|(_, v)| v);
}
let at = self.index.binary_search_by_key(&tag, |e| e.0).ok()?;
self.index.get(at).and_then(|&(_, _, i)| self.fields.get(i as usize)).map(|(_, v)| v)
}
/// Replaces the fields (the root object is filled in after it is pushed) and rebuilds the
+62
View File
@@ -297,6 +297,42 @@ mod archive {
assert_eq!(Archive::open(&file, Limits::default()).unwrap_err(), Error::Malformed("allocation tables form a cycle"));
}
#[test]
fn allocation_records_are_charged_before_they_are_walked() {
let d = doc();
let mut file = synth::container(&[("doc.dat", &d, Method::Stored)], None);
let fat = u64::from_le_bytes(file[16..24].try_into().unwrap()) as usize;
// The table's file count sits after its tag, next, date and 24 reserved bytes.
let files = fat + 4 + 8 + 8 + 24;
file[files..files + 4].copy_from_slice(&u32::MAX.to_le_bytes());
assert_eq!(Archive::open(&file, Limits::default()).unwrap_err(), Error::Limit("too many allocation records"));
// A count within the budget still reaches the records (and here fails on missing data).
file[files..files + 4].copy_from_slice(&1_000_000u32.to_le_bytes());
assert!(matches!(Archive::open(&file, Limits::default()), Err(Error::Malformed(_))));
}
#[test]
fn allocation_records_are_charged_across_every_table() {
let d = doc();
let mut file = synth::container(&[("doc.dat", &d, Method::Stored), ("other.dat", &d, Method::Stored)], None);
let fat = u64::from_le_bytes(file[16..24].try_into().unwrap()) as usize;
// Chain a copy of the (valid, two-record) table after it; the copy is the last table.
let second = file.len();
let table = file[fat..].to_vec();
file.extend(&table);
file[fat + 4..fat + 12].copy_from_slice(&(second as u64).to_le_bytes());
let files = second + 4 + 8 + 8 + 24;
// Each count alone is within the archive budget of 2^25; together they exceed it by one.
file[files..files + 4].copy_from_slice(&((1u32 << 25) - 1).to_le_bytes());
assert_eq!(Archive::open(&file, Limits::default()).unwrap_err(), Error::Limit("too many allocation records"));
// Exactly the budget is allowed: the second table is walked (and fails on missing data).
file[files..files + 4].copy_from_slice(&((1u32 << 25) - 2).to_le_bytes());
assert!(matches!(Archive::open(&file, Limits::default()), Err(Error::Malformed(_))));
// Directory records count too: one more (after the file count and 8 bytes) is over.
file[files + 12..files + 14].copy_from_slice(&1u16.to_le_bytes());
assert_eq!(Archive::open(&file, Limits::default()).unwrap_err(), Error::Limit("too many allocation records"));
}
#[test]
fn hostile_streams_fail_cleanly() {
let deep = (0..2000).fold(Vec::new(), |inner, _| vec![(tag(b"Chld"), F::Obj(tag(b"Grup"), inner))]);
@@ -416,6 +452,20 @@ mod shapes {
rx.recv_timeout(std::time::Duration::from_secs(20)).unwrap_or_else(|_| panic!("reading the document did not finish")).unwrap()
}
#[test]
fn empty_subpaths_of_a_shared_curve_are_charged_for_every_occurrence() {
// One curve of 100k empty subpaths, placed 100 times: no records and no points, but
// ten million subpaths walked unless each one is charged.
let mut data = vec![F::U8(0), F::U32(100_000)];
for _ in 0..100_000 {
data.extend([F::Bool(false), F::Records(18, Vec::new())]);
}
let curve = F::Def(5, vec![tag(b"PCrv")], vec![(tag(b"Crvs"), F::Obj(tag(b"PCvD"), vec![(tag(b"Data"), F::Pos(data))]))]);
let group = F::Def(6, vec![tag(b"Grup")], vec![(tag(b"Chld"), F::Shared((0..100).map(|_| F::Ref(5)).collect()))]);
let d = read(vec![curve, group], 6);
assert!(d.warnings.iter().any(|w| w.contains("curve geometry budget exhausted")), "{:?}", d.warnings);
}
fn path(document: &Document) -> &Path {
match &document.spreads[0].nodes[0].kind {
Kind::Shape { path, .. } => path,
@@ -786,6 +836,18 @@ mod stream_storage {
}
}
#[test]
fn last_lookup_keeps_last_match_for_every_object_size() {
for n in 0..60u32 {
let fields: Vec<(Tag, Value)> = (0..n).map(|i| (if i % 5 == 0 { Tag(0) } else { Tag(100 + i % 7) }, Value::UInt(u64::from(i)))).collect();
let object = Object::new(Tag(1), Kind::Inline, vec![Tag(1)], fields.clone());
for t in 0..120 {
let expected = if t == 0 { None } else { fields.iter().rev().find(|(k, _)| *k == Tag(t)).map(|(_, v)| v) };
assert_eq!(object.get_last(Tag(t)), expected, "{n} fields, tag {t}");
}
}
}
#[test]
fn a_fixed_size_array_is_one_buffer_and_one_value_per_record() {
let records: Vec<u8> = (0..18 * 5).map(|i| i as u8).collect();
+97
View File
@@ -33,9 +33,63 @@ fn map_exr(e: exr::error::Error) -> CodecError {
}
}
/// Refuses headers whose `preview` attributes declare more pixels than their value holds.
///
/// The `exr` crate sizes a preview's pixel buffer from its declared dimensions (up to 4 MiB at
/// once, zero-filled) before it finds the attribute too short, and in the non-pedantic mode
/// every reader here uses it then skips the attribute and goes on: thousands of tiny malformed
/// previews would each cost that allocation. This walks the raw headers (name, type, size,
/// value per attribute; an empty name ends a header, an empty header ends a multi-part list)
/// and checks each preview against its own size, so the crate only reads consistent ones.
/// Anything else odd is left to the crate to report.
pub(crate) fn check_previews(b: &[u8]) -> Result<(), CodecError> {
if b.get(..4) != Some(&[0x76, 0x2f, 0x31, 0x01][..]) {
return Ok(());
}
let Some(flags) = b.get(4..8).and_then(|v| <[u8; 4]>::try_from(v).ok()).map(u32::from_le_bytes) else { return Ok(()) };
let multipart = flags & 0x1000 != 0;
let cstr = |at: usize| -> Option<(&[u8], usize)> {
let rest = b.get(at..)?;
let n = rest.iter().position(|&c| c == 0)?;
Some((rest.get(..n)?, at + n + 1))
};
let u32_at = |at: usize| b.get(at..at.checked_add(4)?).and_then(|v| <[u8; 4]>::try_from(v).ok()).map(u32::from_le_bytes);
let mut at = 8;
loop {
// One header's attributes.
loop {
let Some((name, next)) = cstr(at) else { return Ok(()) };
at = next;
if name.is_empty() {
break;
}
let Some((kind, next)) = cstr(at) else { return Ok(()) };
let Some(size) = u32_at(next).and_then(|n| i32::try_from(n).ok()).and_then(|n| usize::try_from(n).ok()) else { return Ok(()) };
let value = next + 4;
let end = match value.checked_add(size) {
Some(end) if end <= b.len() => end,
_ => return Ok(()),
};
if kind == b"preview" {
let declared =
u32_at(value).zip(u32_at(value + 4)).and_then(|(w, h)| u64::from(w).checked_mul(u64::from(h))?.checked_mul(4)).filter(|_| size >= 8);
if declared.is_none_or(|n| n > (size - 8) as u64) {
return Err(err("a preview attribute declares more pixels than it holds"));
}
}
at = end;
}
// A multi-part list ends with an empty header (a lone NUL).
if !multipart || b.get(at) == Some(&0) {
return Ok(());
}
}
}
/// Read the headers without the `exr` crate's validation (it rejects deep parts outright),
/// after checking every part's declared size against the limits, before any pixel allocation.
pub(super) fn read_meta(bytes: &[u8], limits: &Limits) -> Result<MetaData, CodecError> {
check_previews(bytes)?;
let meta = MetaData::read_from_buffered(Cursor::new(bytes), false).map_err(err)?;
// Flat decoding reads every part at once, so the parts' sizes are also capped together.
let mut total: u64 = 0;
@@ -130,6 +184,7 @@ impl<'s, C: ReadChannels<'s>> ReadChannels<'s> for OnlyPart<C> {
/// Decode one part's flat layer via the `exr` crate (it handles every flat compression).
/// Other parts are not decompressed, so an AOV file costs one part per request, not the file.
pub(super) fn read_layer(bytes: &[u8], index: usize) -> Result<Layer<AnyChannels<FlatSamples>>, CodecError> {
check_previews(bytes)?;
let only = OnlyPart { inner: read().no_deep_data().largest_resolution_level().all_channels(), index, seen: std::cell::Cell::new(0) };
only.first_valid_layer().all_attributes().from_buffered(Cursor::new(bytes)).map(|image| image.layer_data).map_err(map_exr)
}
@@ -334,3 +389,45 @@ pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result<Ve
image.write().to_buffered(&mut cursor).map_err(|e| CodecError::encode(F, e))?;
Ok(cursor.into_inner())
}
#[cfg(test)]
mod preview_tests {
use super::*;
use exr::meta::attribute::Preview;
use exr::prelude::{ImageAttributes, IntegerBounds};
/// A 2x2 single-part file whose layer carries a 2x1 preview.
fn with_preview() -> Vec<u8> {
let mut attrs = LayerAttributes::named("rgb");
attrs.preview = Some(Preview { size: exr::math::Vec2(2, 1), pixel_data: vec![7; 8] });
let list = ["R", "G", "B"].into_iter().map(|n| AnyChannel::new(n, FlatSamples::F32(vec![0.5; 4]))).collect();
let layer = Layer::new(
(2, 2),
attrs,
Encoding { compression: Compression::Uncompressed, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing },
AnyChannels::sort(list),
);
let image = exr::image::Image::from_layers(ImageAttributes::new(IntegerBounds::new((0, 0), (2, 2))), vec![layer]);
let mut out = Cursor::new(Vec::new());
image.write().to_buffered(&mut out).unwrap();
out.into_inner()
}
#[test]
fn a_preview_declaring_more_pixels_than_it_holds_is_refused_before_the_crate_reads_it() {
let good = with_preview();
check_previews(&good).unwrap();
assert_eq!(decode(&good, &Limits::default()).unwrap().dimensions(), (2, 2));
// Patch the preview to 1024x1024 (4 MiB) while its value still holds 8 bytes.
let tag = b"preview\0preview\0";
let at = good.windows(tag.len()).position(|w| w == tag).unwrap() + tag.len() + 4;
let mut bad = good.clone();
bad[at..at + 4].copy_from_slice(&1024u32.to_le_bytes());
bad[at + 4..at + 8].copy_from_slice(&1024u32.to_le_bytes());
for r in [check_previews(&bad), decode(&bad, &Limits::default()).map(|_| ()), read_layer(&bad, 0).map(|_| ())] {
assert!(matches!(&r, Err(CodecError::Malformed { message, .. }) if message.contains("preview")), "{r:?}");
}
// A header cut off mid-attribute is left to the crate.
check_previews(&good[..at]).unwrap();
}
}
@@ -148,6 +148,7 @@ fn layer_streams(header: &exr::meta::header::Header, name: &str) -> Vec<String>
/// Lists every Cryptomatte layer of the file from the headers alone: what a Cryptomatte
/// picker offers. The embedded manifest (capped, JSON) is parsed into (ID, name) pairs.
pub(crate) fn layers(bytes: &[u8], limits: &Limits) -> Result<Vec<CryptomatteLayer>, CodecError> {
super::exr::check_previews(bytes)?;
let meta = MetaData::read_from_buffered(Cursor::new(bytes), false).map_err(|e| CodecError::malformed(F, e))?;
// Manifests can be large; bound the embedded text before parsing anything.
const MANIFEST_CAP: usize = 16 << 20;
+1
View File
@@ -275,6 +275,7 @@ pub fn decode_exr_channels(bytes: &[u8], part: usize, names: &[&str], opts: &Dec
/// [`decode_as_with`] instead flattens a deep file into a normal [`Image`] with a
/// [`DecodeWarning::DeepFlattened`]. `Err(Unsupported)` when the file has no deep part.
pub fn decode_deep_exr(bytes: &[u8], limits: &Limits) -> Result<DeepImage, CodecError> {
codecs::exr::check_previews(bytes)?;
let mut cur = std::io::Cursor::new(bytes);
let meta = ::exr::meta::MetaData::read_from_buffered(&mut cur, false).map_err(|e| CodecError::malformed(Format::OpenExr, e))?;
codecs::deep_exr::decode_deep(&meta, bytes, limits)