Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

security(psd): check padding length before scanning it

Dylan M. Taylord0ad7ddb6fa0
diff --git a/crates/psd/src/filter_effects.rs b/crates/psd/src/filter_effects.rs
index e8b5efb69..cadb3ac8d 100644
--- a/crates/psd/src/filter_effects.rs
+++ b/crates/psd/src/filter_effects.rs
@@ -170,7 +170,10 @@ impl FilterEffects {
let pad = ((4 - len % 4) % 4).min(r.remaining() as u64);
r.skip(pad as usize)?;
let rest = r.peek_rest();
- if rest.iter().all(|&b| b == 0) && rest.len() <= 3 {
+ // Padding is at most three bytes. Check the length before scanning the suffix for
+ // zeros: otherwise each iteration scans the whole remaining buffer, which is
+ // quadratic when a block contains many empty sections.
+ if rest.len() <= 3 && rest.iter().all(|&b| b == 0) {
break;
}
if rest.len() < 8 {