Skip to content

Commit

Permalink
Fix C++ slint_register_font_from_* functions regarding to the error…
Browse files Browse the repository at this point in the history
… string

The C++ code already instentiate a `SharedString` on the stack, we are
not supposed to ptr::write into it.
Lickyly this didn't have any effect because the default constructed
string can be leaked without problem, but better be correct.

Also aboud temporary String allocation
  • Loading branch information
ogoffart committed Jan 27, 2025
1 parent c98d234 commit 62e9111
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions api/cpp/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,34 +104,30 @@ pub unsafe extern "C" fn slint_quit_event_loop() {
pub unsafe extern "C" fn slint_register_font_from_path(
win: *const WindowAdapterRcOpaque,
path: &SharedString,
error_str: *mut SharedString,
error_str: &mut SharedString,
) {
let window_adapter = &*(win as *const Rc<dyn WindowAdapter>);
core::ptr::write(
error_str,
match window_adapter.renderer().register_font_from_path(std::path::Path::new(path.as_str()))
{
Ok(()) => Default::default(),
Err(err) => err.to_string().into(),
},
)
*error_str = match window_adapter
.renderer()
.register_font_from_path(std::path::Path::new(path.as_str()))
{
Ok(()) => Default::default(),
Err(err) => i_slint_core::string::ToSharedString::to_shared_string(&err),
};
}

#[cfg(feature = "std")]
#[no_mangle]
pub unsafe extern "C" fn slint_register_font_from_data(
win: *const WindowAdapterRcOpaque,
data: i_slint_core::slice::Slice<'static, u8>,
error_str: *mut SharedString,
error_str: &mut SharedString,
) {
let window_adapter = &*(win as *const Rc<dyn WindowAdapter>);
core::ptr::write(
error_str,
match window_adapter.renderer().register_font_from_memory(data.as_slice()) {
Ok(()) => Default::default(),
Err(err) => err.to_string().into(),
},
)
*error_str = match window_adapter.renderer().register_font_from_memory(data.as_slice()) {
Ok(()) => Default::default(),
Err(err) => i_slint_core::string::ToSharedString::to_shared_string(&err),
};
}

#[no_mangle]
Expand Down

0 comments on commit 62e9111

Please sign in to comment.