From 712d463f825c11e11a9a6ca64f376b4e1f2f9420 Mon Sep 17 00:00:00 2001 From: John Turnbach Date: Fri, 23 Oct 2026 11:33:56 -0400 Subject: [PATCH] fix(io): read linked smart objects for PSD export as regular files only Saving a PSD embeds the file a linked smart object names, and it read that file with a bare std::fs::read. A link that names a FIFO blocked the save forever (opening a FIFO for reading waits for a writer), and one that names a device such as /dev/zero read until memory ran out. Every other linked-file read already goes through photocraft_format::read_file, which opens without blocking, refuses anything that is not a regular file and reads in bounded chunks; the PSD exporter now uses it too. Such a layer is written as pixels with a warning that says why, as for any other unreadable link. The new test psd_export::tests::a_linked_fifo_is_not_read exports a document whose linked smart object points at a FIFO. Before the fix the export never returned (the test times out after 30 s); now it finishes and warns that the path is not a regular file. Fixes #8601 --- crates/io/src/psd_export.rs | 39 ++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/crates/io/src/psd_export.rs b/crates/io/src/psd_export.rs index fd07adde50e..e8ae5837889 100644 --- a/crates/io/src/psd_export.rs +++ b/crates/io/src/psd_export.rs @@ -988,7 +988,9 @@ impl Ex { return Err(format!("the linked file {path} is not read for this export")); } let file = crate::doc_path::resolve(path, self.document.as_deref()).map_err(|e| format!("the linked file {path} is not opened: {e}"))?; - let bytes = std::fs::read(file).map_err(|e| format!("the linked file {path} can't be read: {e}"))?; + // Regular files only, in bounded reads: a link to a FIFO would block the save + // and one to a device such as `/dev/zero` would never end (#8601). + let bytes = photocraft_format::read_file(&file).map_err(|e| format!("the linked file {path} can't be read: {e}"))?; let name = path.rsplit(['/', '\\']).next().unwrap_or(path).to_string(); self.warnings.push(format!("the linked smart object {name} was embedded (PSD export keeps no external links)")); self.embed(sm.contents_id, &name, &bytes) @@ -2218,4 +2220,39 @@ mod tests { assert_eq!(back.channels[0].name, "Alpha 1"); assert!((back.channels[0].opacity - 0.5).abs() < 1e-6); } + + /// A linked smart object whose path names a FIFO is not read (#8601): opening a FIFO for + /// reading blocks until a writer appears, and a device such as `/dev/zero` never ends, so the + /// export would hang or run out of memory. The export finishes and writes the layer as pixels, + /// saying why. The export runs on its own thread so that a regression fails here instead of + /// hanging the test run. + #[cfg(unix)] + #[test] + fn a_linked_fifo_is_not_read() { + let dir = std::env::temp_dir().join(format!("photocraft-linked-fifo-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let fifo = dir.join("link.png"); + let _ = std::fs::remove_file(&fifo); + assert!(std::process::Command::new("mkfifo").arg(&fifo).status().unwrap().success(), "mkfifo failed"); + let mut doc = document(8, 6); + let smart = photocraft_doc::SmartObject { + contents_id: SmartContentsId::fresh(), + source: photocraft_doc::SmartSource::Linked { path: fifo.to_string_lossy().into_owned() }, + transform: photocraft_geom::Affine::IDENTITY, + smart_filters: vec![], + cache: Some(Surface::new(doc.pixel_format())), + psd_raw: None, + filters_enabled: true, + filter_mask: None, + warp: None, + stack_mode: None, + perspective: None, + }; + doc.layers.push(Layer::new("Linked", LayerContent::Smart(smart))); + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || tx.send(document_to_psd_with(&doc, &PsdExportOptions::default()).1)); + let warnings = rx.recv_timeout(std::time::Duration::from_secs(30)).expect("the export blocked reading the FIFO"); + let _ = std::fs::remove_dir_all(&dir); + assert!(warnings.iter().any(|w| w.contains("smart object written as pixels") && w.contains("not a regular file")), "{warnings:?}"); + } }