Skip to content

Commit b02db5e

Browse files
authored
Merge pull request #3123 from hammerlink/master
Prevent crashes from `svg` rendering
2 parents 7825311 + 4cc7eb4 commit b02db5e

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

tiny_skia/src/vector.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use tiny_skia::Transform;
88
use std::cell::RefCell;
99
use std::collections::hash_map;
1010
use std::fs;
11+
use std::panic;
1112
use std::sync::Arc;
1213

1314
#[derive(Debug)]
@@ -171,7 +172,15 @@ impl Cache {
171172
tiny_skia::Transform::default()
172173
};
173174

174-
resvg::render(tree, transform, &mut image.as_mut());
175+
// SVG rendering can panic on malformed or complex vectors.
176+
// We catch panics to prevent crashes and continue gracefully.
177+
let render = panic::catch_unwind(panic::AssertUnwindSafe(|| {
178+
resvg::render(tree, transform, &mut image.as_mut());
179+
}));
180+
181+
if let Err(error) = render {
182+
log::warn!("SVG rendering for {handle:?} panicked: {error:?}");
183+
}
175184

176185
if let Some([r, g, b, _]) = key.color {
177186
// Apply color filter

wgpu/src/image/vector.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use resvg::tiny_skia;
66
use resvg::usvg;
77
use rustc_hash::{FxHashMap, FxHashSet};
88
use std::fs;
9+
use std::panic;
910
use std::sync::Arc;
1011

1112
/// Entry in cache corresponding to an svg handle
@@ -154,7 +155,18 @@ impl Cache {
154155
tiny_skia::Transform::default()
155156
};
156157

157-
resvg::render(tree, transform, &mut img.as_mut());
158+
// SVG rendering can panic on malformed or complex vectors.
159+
// We catch panics to prevent crashes and continue gracefully.
160+
let render =
161+
panic::catch_unwind(panic::AssertUnwindSafe(|| {
162+
resvg::render(tree, transform, &mut img.as_mut());
163+
}));
164+
165+
if let Err(error) = render {
166+
log::warn!(
167+
"SVG rendering for {handle:?} panicked: {error:?}"
168+
);
169+
}
158170

159171
let mut rgba = img.take();
160172

0 commit comments

Comments
 (0)