fix: prevent crashes from SVG rendering

This commit is contained in:
Hendrik Hamerlinck 2025-11-23 12:39:22 +01:00
parent 8bfd099c59
commit d845ac27db
No known key found for this signature in database
GPG key ID: 34BCF97CC5893BCF
2 changed files with 30 additions and 4 deletions

View file

@ -7,7 +7,7 @@ use tiny_skia::Transform;
use std::cell::RefCell;
use std::collections::hash_map;
use std::fs;
use std::{fs, panic};
use std::sync::Arc;
#[derive(Debug)]
@ -168,7 +168,20 @@ impl Cache {
tiny_skia::Transform::default()
};
resvg::render(tree, transform, &mut image.as_mut());
// SVG rendering can panic on malformed or complex vectors.
// We catch panics to prevent crashes and continue gracefully.
let render_result =
panic::catch_unwind(panic::AssertUnwindSafe(|| {
resvg::render(tree, transform, &mut image.as_mut());
}));
if render_result.is_err() {
log::warn!(
"SVG rendering panicked for handle ID: {}",
handle.id()
);
return None;
}
if let Some([r, g, b, _]) = key.color {
// Apply color filter

View file

@ -5,7 +5,7 @@ use crate::image::atlas::{self, Atlas};
use resvg::tiny_skia;
use resvg::usvg;
use rustc_hash::{FxHashMap, FxHashSet};
use std::fs;
use std::{fs, panic};
use std::sync::Arc;
/// Entry in cache corresponding to an svg handle
@ -154,7 +154,20 @@ impl Cache {
tiny_skia::Transform::default()
};
resvg::render(tree, transform, &mut img.as_mut());
// SVG rendering can panic on malformed or complex vectors.
// We catch panics to prevent crashes and continue gracefully.
let render_result =
panic::catch_unwind(panic::AssertUnwindSafe(|| {
resvg::render(tree, transform, &mut img.as_mut());
}));
if render_result.is_err() {
log::warn!(
"SVG rendering panicked for handle ID: {}",
handle.id()
);
return None;
}
let mut rgba = img.take();