Skip to content

resvg_render() (C wrapper function) does not crash on incorrect input #926

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/c-api/ResvgQt.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,9 @@ class ResvgRenderer {

QImage qImg(svgSize.width(), svgSize.height(), QImage::Format_ARGB32_Premultiplied);
qImg.fill(Qt::transparent);
resvg_render(d->tree, ts, qImg.width(), qImg.height(), (char*)qImg.bits());
if (!resvg_render(d->tree, ts, qImg.width(), qImg.height(), (char*)qImg.bits())) {
return QImage();
}

// resvg renders onto the RGBA canvas, while QImage is ARGB.
// std::move is required to call inplace version of rgbSwapped().
Expand Down
26 changes: 16 additions & 10 deletions crates/c-api/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use std::ffi::CStr;
use std::os::raw::c_char;
use std::panic::AssertUnwindSafe;
use std::slice;

use resvg::tiny_skia;
Expand Down Expand Up @@ -868,25 +869,30 @@ fn convert_error(e: usvg::Error) -> resvg_error {
/// @param height Pixmap height.
/// @param pixmap Pixmap data. Should have width*height*4 size and contain
/// premultiplied RGBA8888 pixels.
/// @return `true` if the rendering was successful; `false` elsewhere
#[no_mangle]
pub extern "C" fn resvg_render(
tree: *const resvg_render_tree,
transform: resvg_transform,
width: u32,
height: u32,
pixmap: *mut c_char,
) {
let tree = unsafe {
assert!(!tree.is_null());
&*tree
};
) -> bool {
if tree.is_null() || pixmap.is_null() || width <= 0 || height <= 0 {
return false;
}

let pixmap_len = width as usize * height as usize * tiny_skia::BYTES_PER_PIXEL;
let pixmap: &mut [u8] =
unsafe { std::slice::from_raw_parts_mut(pixmap as *mut u8, pixmap_len) };
let mut pixmap = tiny_skia::PixmapMut::from_bytes(pixmap, width, height).unwrap();
std::panic::catch_unwind(AssertUnwindSafe(|| {
let tree = unsafe { &*tree };

resvg::render(&tree.0, transform.to_tiny_skia(), &mut pixmap)
let pixmap_len = width as usize * height as usize * tiny_skia::BYTES_PER_PIXEL;
let pixmap: &mut [u8] =
unsafe { std::slice::from_raw_parts_mut(pixmap as *mut u8, pixmap_len) };
let mut pixmap = tiny_skia::PixmapMut::from_bytes(pixmap, width, height).unwrap();

resvg::render(&tree.0, transform.to_tiny_skia(), &mut pixmap)
}))
.is_ok()
}

/// @brief Renders a Node by ID onto the image.
Expand Down
3 changes: 2 additions & 1 deletion crates/c-api/resvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,9 @@ void resvg_tree_destroy(resvg_render_tree *tree);
* @param height Pixmap height.
* @param pixmap Pixmap data. Should have width*height*4 size and contain
* premultiplied RGBA8888 pixels.
* @return `true` if the rendering was successful; `false` elsewhere
*/
void resvg_render(const resvg_render_tree *tree,
bool resvg_render(const resvg_render_tree *tree,
resvg_transform transform,
uint32_t width,
uint32_t height,
Expand Down