#1132: a damaged PSD layer section keeps the merged image instead of failing the document

Psd::parse drops the layers, records Psd::warnings and keeps the header and
merged image; the importer then imports the flattened image as one layer and
reports 'Layers could not be read'. Header damage and a missing merged image
stay hard errors.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
vangel76
2026-10-12 23:00:40 +02:00
co-authored by Claude Sonnet 5.5
parent 470ea7a3af
commit 6259bf0b57
4 changed files with 137 additions and 3 deletions
+19 -2
View File
@@ -339,6 +339,9 @@ pub struct Psd {
pub palette: Vec<u8>,
/// Files embedded for smart objects.
pub linked: Vec<LinkedFile>,
/// Repairs made while reading: damage that was skipped instead of failing the document
/// (for example an unreadable layer section, in which case only the merged image is kept).
pub warnings: Vec<String>,
data: Arc<[u8]>,
merged_offset: usize,
}
@@ -387,6 +390,7 @@ impl Psd {
merged_alpha: false,
palette,
linked: vec![],
warnings: vec![],
data: data.clone(),
merged_offset: 0,
};
@@ -395,8 +399,17 @@ impl Psd {
if lm_len > 0 {
let li_len = r.length(psb)?;
let li_end = r.pos.checked_add(li_len).filter(|&e| e <= lm_end).ok_or(Error::Truncated)?;
if li_len > 0 {
psd.layer_info(&data, r.pos, li_end)?;
if li_len > 0
&& let Err(e) = psd.layer_info(&data, r.pos, li_end)
{
// A damaged layer section must not discard the document: keep the header and the
// merged image, drop the layers and say so. Without a merged image there is
// nothing to fall back to, so the original error stands.
if lm_end.checked_add(2).is_none_or(|n| n > data.len()) {
return Err(e);
}
psd.layers.clear();
psd.warnings.push(format!("Layers could not be read ({e}); only the flattened image is available"));
}
r.pos = li_end;
if r.pos + 4 <= lm_end {
@@ -422,6 +435,10 @@ impl Psd {
r.pos = end;
}
}
if !psd.layers.is_empty() {
// A 16/32-bit layer block recovered the layers.
psd.warnings.clear();
}
psd.merged_offset = lm_end;
Ok(psd)
}