Fix C++ slint_register_font_from_* functions regarding to the error 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
This commit is contained in:
Olivier Goffart 2025-01-27 18:30:27 +01:00
parent c98d234b9e
commit 62e9111842

View file

@ -104,17 +104,16 @@ 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")]
@ -122,16 +121,13 @@ pub unsafe extern "C" fn slint_register_font_from_path(
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]