Fix cancellable PDN and XCF imports

This commit is contained in:
Dylan M. Taylor committed 2026-11-12 23:46:00 +00:00
1 parent 8f780afad2
commit 0d24e1cdc6
9 files changed
+68 -52

No files matched your search

Generated
+12 -12
View File
@@ -1434,7 +1434,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba62
[[package]]
name = "craft-dpx"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
source = "git+https://github.com/storytold/craft-libs?rev=f92a91afa400883309aa46d8a3c0d0baa31c4f35#f92a91afa400883309aa46d8a3c0d0baa31c4f35"
dependencies = [
"thiserror 2.0.21",
]
@@ -1442,7 +1442,7 @@ dependencies = [
[[package]]
name = "craft-exr"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
source = "git+https://github.com/storytold/craft-libs?rev=f92a91afa400883309aa46d8a3c0d0baa31c4f35#f92a91afa400883309aa46d8a3c0d0baa31c4f35"
dependencies = [
"serde_json",
]
@@ -1455,7 +1455,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba62
[[package]]
name = "craft-gif"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
source = "git+https://github.com/storytold/craft-libs?rev=f92a91afa400883309aa46d8a3c0d0baa31c4f35#f92a91afa400883309aa46d8a3c0d0baa31c4f35"
[[package]]
name = "craft-heif"
@@ -1500,7 +1500,7 @@ dependencies = [
[[package]]
name = "craft-kra"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
source = "git+https://github.com/storytold/craft-libs?rev=f92a91afa400883309aa46d8a3c0d0baa31c4f35#f92a91afa400883309aa46d8a3c0d0baa31c4f35"
dependencies = [
"craft-xml",
"craft-zip",
@@ -1510,7 +1510,7 @@ dependencies = [
[[package]]
name = "craft-ora"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
source = "git+https://github.com/storytold/craft-libs?rev=f92a91afa400883309aa46d8a3c0d0baa31c4f35#f92a91afa400883309aa46d8a3c0d0baa31c4f35"
dependencies = [
"craft-xml",
"craft-zip",
@@ -1542,7 +1542,7 @@ dependencies = [
[[package]]
name = "craft-pdn"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
source = "git+https://github.com/storytold/craft-libs?rev=f92a91afa400883309aa46d8a3c0d0baa31c4f35#f92a91afa400883309aa46d8a3c0d0baa31c4f35"
dependencies = [
"flate2",
"thiserror 2.0.21",
@@ -1565,7 +1565,7 @@ dependencies = [
[[package]]
name = "craft-psd"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
source = "git+https://github.com/storytold/craft-libs?rev=f92a91afa400883309aa46d8a3c0d0baa31c4f35#f92a91afa400883309aa46d8a3c0d0baa31c4f35"
dependencies = [
"encoding_rs",
"flate2",
@@ -1576,7 +1576,7 @@ dependencies = [
[[package]]
name = "craft-raster-codecs"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
source = "git+https://github.com/storytold/craft-libs?rev=f92a91afa400883309aa46d8a3c0d0baa31c4f35#f92a91afa400883309aa46d8a3c0d0baa31c4f35"
dependencies = [
"rayon",
"thiserror 2.0.21",
@@ -1585,7 +1585,7 @@ dependencies = [
[[package]]
name = "craft-tga"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
source = "git+https://github.com/storytold/craft-libs?rev=f92a91afa400883309aa46d8a3c0d0baa31c4f35#f92a91afa400883309aa46d8a3c0d0baa31c4f35"
dependencies = [
"thiserror 2.0.21",
]
@@ -1649,7 +1649,7 @@ dependencies = [
[[package]]
name = "craft-xcf"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
source = "git+https://github.com/storytold/craft-libs?rev=f92a91afa400883309aa46d8a3c0d0baa31c4f35#f92a91afa400883309aa46d8a3c0d0baa31c4f35"
dependencies = [
"flate2",
"thiserror 2.0.21",
@@ -1658,7 +1658,7 @@ dependencies = [
[[package]]
name = "craft-xml"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
source = "git+https://github.com/storytold/craft-libs?rev=f92a91afa400883309aa46d8a3c0d0baa31c4f35#f92a91afa400883309aa46d8a3c0d0baa31c4f35"
dependencies = [
"quick-xml 0.41.0",
"thiserror 2.0.21",
@@ -1667,7 +1667,7 @@ dependencies = [
[[package]]
name = "craft-zip"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
source = "git+https://github.com/storytold/craft-libs?rev=f92a91afa400883309aa46d8a3c0d0baa31c4f35#f92a91afa400883309aa46d8a3c0d0baa31c4f35"
dependencies = [
"flate2",
"thiserror 2.0.21",
+10 -10
View File
@@ -19,7 +19,7 @@ photocraft-raster = { path = "crates/raster" }
photocraft-doc = { path = "crates/doc" }
photocraft-ops = { path = "crates/ops" }
# PSD/PSB and the Photoshop resource files (ABR, ASL, ACO, ATN, ...): the shared reader and writer (craft-libs#875).
craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "f92a91afa400883309aa46d8a3c0d0baa31c4f35" }
photocraft-codecs = { path = "crates/codecs" }
photocraft-raw = { path = "crates/raw" }
photocraft-affinity = { path = "crates/affinity" }
@@ -42,21 +42,21 @@ photocraft-ipp = { path = "crates/ipp" }
# external
# Krita, OpenRaster and GIMP XCF: the format code (archives, node trees, tiles); the mapping onto
# documents stays in crates/io (craft-libs#928, #935, #960).
craft-kra = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
craft-ora = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
craft-xcf = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
craft-kra = { git = "https://github.com/storytold/craft-libs", rev = "f92a91afa400883309aa46d8a3c0d0baa31c4f35" }
craft-ora = { git = "https://github.com/storytold/craft-libs", rev = "f92a91afa400883309aa46d8a3c0d0baa31c4f35" }
craft-xcf = { git = "https://github.com/storytold/craft-libs", rev = "f92a91afa400883309aa46d8a3c0d0baa31c4f35" }
# Paint.NET PDN3 and its MS-NRBF serialization (craft-libs#968).
craft-pdn = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
craft-pdn = { git = "https://github.com/storytold/craft-libs", rev = "f92a91afa400883309aa46d8a3c0d0baa31c4f35" }
# DDS, DICOM, FITS, IFF/ILBM, PCX, PNM and XPM: the shared decoders and encoders (craft-libs#880).
# DPX and Cineon frames (craft-libs#887); the mapping onto images stays in crates/codecs.
craft-dpx = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
craft-dpx = { git = "https://github.com/storytold/craft-libs", rev = "f92a91afa400883309aa46d8a3c0d0baa31c4f35" }
# The Targa writer (craft-libs#892).
craft-tga = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
craft-tga = { git = "https://github.com/storytold/craft-libs", rev = "f92a91afa400883309aa46d8a3c0d0baa31c4f35" }
# GIF blocks without decoding: the first frame's colour table, the frame walk (craft-libs#939).
craft-gif = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
craft-gif = { git = "https://github.com/storytold/craft-libs", rev = "f92a91afa400883309aa46d8a3c0d0baa31c4f35" }
# OpenEXR channel naming and Cryptomatte IDs, manifests and streams (craft-libs#929).
craft-exr = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
craft-raster-codecs = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
craft-exr = { git = "https://github.com/storytold/craft-libs", rev = "f92a91afa400883309aa46d8a3c0d0baa31c4f35" }
craft-raster-codecs = { git = "https://github.com/storytold/craft-libs", rev = "f92a91afa400883309aa46d8a3c0d0baa31c4f35" }
craft-print = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
# HEIC/HEIF: the shared decoder (replaces crates/heif; craft-libs#784).
craft-heif = { git = "https://github.com/storytold/craft-libs", rev = "081078bb1b44aeedfa5bef3d4f13d978dc77510a" }
+2 -2
View File
@@ -119,7 +119,7 @@ impl CmykModel {
(v + 4.0 * self.tvi[i] * v * (1.0 - v)).clamp(0.0, 1.0)
});
let mut s = [0.0; 3];
for m in 0..16 {
for (m, prim) in self.prim_n.iter().enumerate() {
let mut w = 1.0;
for (i, ai) in a.iter().enumerate() {
w *= if m & (1 << i) != 0 { *ai } else { 1.0 - ai };
@@ -127,7 +127,7 @@ impl CmykModel {
if w == 0.0 {
continue;
}
for (acc, p) in s.iter_mut().zip(&self.prim_n[m]) {
for (acc, p) in s.iter_mut().zip(prim) {
*acc += w * p;
}
}
+3 -3
View File
@@ -211,11 +211,11 @@ fn deeply_nested_maindoc_is_an_error_not_a_stack_overflow() {
let entity = format!("<!DOCTYPE DOC [<!ENTITY e \"{open}{close}\">]><DOC><IMAGE width=\"4\" height=\"4\">&e;</IMAGE></DOC>");
// A quoted `?>` in the XML declaration must not hide the DOCTYPE behind it.
let declared = format!("<?xml version=\"?>\"?>{entity}");
// The nesting is refused by its depth; an internal subset is refused outright (craft-kra).
for (xml, why) in [(direct, Some("nest")), (entity, None), (declared, None)] {
// The nesting is refused by its depth; entity-based forms must also fail safely.
for (xml, expected) in [(direct, Some("nest")), (entity, None), (declared, None)] {
let bytes = archive(&xml, &[]);
let e = std::thread::Builder::new().stack_size(512 * 1024).spawn(move || crate::import("deep.kra", &bytes).err()).unwrap().join().unwrap();
assert!(matches!(&e, Some(IoError::Kra(m)) if why.is_none_or(|w| m.contains(w))), "{why:?}: {e:?}");
assert!(matches!(&e, Some(IoError::Kra(m)) if expected.is_none_or(|why| m.contains(why))), "{expected:?}: {e:?}");
}
}
+7 -11
View File
@@ -62,17 +62,12 @@ pub(super) fn import(name: &str, bytes: &[u8], ctl: &Interrupt<'_>) -> Result<Im
}
let count = doc.surface_count();
let mut surfaces = HashMap::new();
let mut failed = None;
doc.read_surfaces(&keep_going, &mut |i, s| {
if failed.is_some() {
return;
}
doc.try_read_surfaces(&keep_going, &mut |i, s| -> Result<()> {
let mut surface = Surface::new(PixelFormat::RGBA8);
let mut row = Vec::with_capacity(width * 4);
for (y, src) in s.rows().enumerate() {
if ctl.check().is_err() {
failed = Some(IoError::Cancelled);
return;
return Err(IoError::Cancelled);
}
row.clear();
row.extend_from_slice(src);
@@ -83,11 +78,12 @@ pub(super) fn import(name: &str, bytes: &[u8], ctl: &Interrupt<'_>) -> Result<Im
}
surfaces.insert(i, surface);
ctl.progress(0.05 + 0.95 * (i + 1) as f32 / count as f32);
Ok(())
})
.map_err(pdn_err)?;
if let Some(e) = failed {
return Err(e);
}
.map_err(|error| match error {
craft_pdn::ReadSurfacesError::Decode(error) => pdn_err(error),
craft_pdn::ReadSurfacesError::Callback(error) => error,
})?;
let mut document = Document::new(name, Size::new(w, h), ColorMode::Rgb, SampleType::U8);
for l in &doc.layers {
let mut layer = Layer::raster(l.name.clone(), PixelFormat::RGBA8);
+2 -2
View File
@@ -153,7 +153,8 @@ fn write_pixels(surface: &mut Surface, px: &Pixels, x: i32, y: i32, budget: &mut
/// Imports an XCF file (already inflated when it was gzipped).
pub(crate) fn import(name: &str, bytes: &[u8], ctl: &Interrupt<'_>) -> Result<ImportResult> {
let xcf = Xcf::parse(bytes).map_err(xcf_err)?;
let keep_going = || ctl.check().is_ok();
let xcf = Xcf::parse_with(bytes, &keep_going).map_err(xcf_err)?;
let info = xcf.image();
let version = info.version;
let mut warnings = Warnings::default();
@@ -208,7 +209,6 @@ pub(crate) fn import(name: &str, bytes: &[u8], ctl: &Interrupt<'_>) -> Result<Im
// Channels: the selection and the extras.
let mut channels = Vec::new();
let mut selection = None;
let keep_going = || ctl.check().is_ok();
for i in 0..info.channel_count {
ctl.check().map_err(|_| IoError::Cancelled)?;
let (channel, px) = xcf.channel(i, &mut reader.budget, &keep_going).map_err(xcf_err)?;
+17
View File
@@ -331,6 +331,23 @@ fn a_layer_decodes_with_every_compression() {
}
}
#[test]
fn xcf_import_can_be_cancelled_while_parsing_layer_metadata() {
use std::sync::atomic::{AtomicUsize, Ordering};
let mut img = TestImage::rgb8(3, 3);
img.layers.push(TestLayer::rgba("first", 3, 3, rgba(3, 3, |_, _| [1, 2, 3, 255])));
img.layers.push(TestLayer::rgba("second", 3, 3, rgba(3, 3, |_, _| [4, 5, 6, 255])));
let bytes = img.write();
let checks = AtomicUsize::new(0);
let cancel = || checks.fetch_add(1, Ordering::Relaxed) >= 1;
let result = super::import("cancel.xcf", &bytes, &Interrupt::cancel_only(&cancel));
assert!(matches!(result, Err(IoError::Cancelled)));
assert_eq!(checks.load(Ordering::Relaxed), 2, "cancellation stops before parsing the second layer");
}
#[test]
fn every_truncation_errors_never_panics() {
let mut img = TestImage::rgb8(20, 20);
+8 -6
View File
@@ -41,12 +41,14 @@ children (checked against Krita's merged image).
## Limits and safety
The reader uses the bounded ZIP reader of `photocraft-format` (no ZIP64, CRC-checked), parses
`maindoc.xml` with `roxmltree` (32 MiB cap; the DTD is declared by public id and not fetched),
and decodes tiles with a bounds-checked LZF decoder that must produce exactly one tile. Tiles
are at most 1024 px a side; canvases at most 300,000 px a side and 1 GiB of decoded pixels;
8,192 nodes; nesting at most `MAX_GROUP_DEPTH`. Version 1 tile files (Krita 1.x) are refused.
Malformed archives, XML, numbers, tile headers and tile data return errors; truncated and
The shared `craft-kra` reader uses `craft-zip` for the archive (including ZIP64 directories,
CRC-checked entry reads, and at most 100,000 entries) and `craft-xml` for `maindoc.xml`. The XML
has a 32 MiB input limit and 512-level nesting limit; Krita's public DTD is not fetched, and an
internal DTD subset is refused. A layer tile file may inflate to at most 1 GiB, and an ICC profile
to 16 MiB. The app decodes tiles with a bounds-checked LZF decoder that must produce exactly one
tile. Tiles are at most 1024 px a side; canvases at most 300,000 px a side and 1 GiB of decoded
pixels; 8,192 nodes; nesting at most `MAX_GROUP_DEPTH`. Version 1 tile files (Krita 1.x) are
refused. Malformed archives, XML, numbers, tile headers and tile data return errors; truncated and
corrupted files are covered by tests.
## Verification
+7 -6
View File
@@ -49,12 +49,13 @@ Hard Mix open as Photoshop's Soft Light and Hard Mix, which differ slightly, wit
## Limits and safety
The reader takes the archive with the bounded ZIP reader of `photocraft-format` (no ZIP64,
CRC-checked), parses `stack.xml` with `roxmltree` (16 MiB cap), and decodes each layer image
through `photocraft-codecs` with size limits. Canvases are at most 300,000 px a side, at most
8,192 layers and groups, nesting at most `MAX_GROUP_DEPTH` deep, and 1 GiB of decoded pixels in
total. Malformed archives, XML, numbers and images return errors; truncated and corrupted files
are covered by tests.
The shared `craft-ora` reader and writer use `craft-zip` for the archive (including ZIP64
directories and CRC-checked entry reads, with at most 100,000 entries) and `craft-xml` for
`stack.xml`. The XML has a 16 MiB input limit and 512-level nesting limit. A layer image entry may
inflate to at most 512 MiB; decoded pixels across the document are limited to 1 GiB. Canvases are
at most 300,000 px a side, at most 8,192 layers and groups, and nesting at most
`MAX_GROUP_DEPTH` deep. Malformed archives, XML, numbers and images return errors; truncated and
corrupted files are covered by tests.
## Verification