fix(ffi): support custom allocators in event string getters

Event string getters returned Rust-allocated pointers that dc_str_unref()
frees with libc. This breaks with custom Rust allocators, so allocate the
strings with strdup() instead.
This commit is contained in:
pancake
2026-09-09 10:54:25 +00:00
committed by l
parent b0e5b08531
commit 4bf42c0af9
+9 -30
View File
@@ -690,7 +690,7 @@ pub unsafe extern "C" fn dc_event_get_data1_str(event: *mut dc_event_t) -> *mut
match event { match event {
EventType::IncomingWebxdcNotify { href, .. } => { EventType::IncomingWebxdcNotify { href, .. } => {
if let Some(href) = href { if let Some(href) = href {
href.to_c_string().unwrap_or_default().into_raw() href.strdup()
} else { } else {
ptr::null_mut() ptr::null_mut()
} }
@@ -719,10 +719,7 @@ pub unsafe extern "C" fn dc_event_get_data2_str(event: *mut dc_event_t) -> *mut
| EventType::DeletedBlobFile(msg) | EventType::DeletedBlobFile(msg)
| EventType::Warning(msg) | EventType::Warning(msg)
| EventType::Error(msg) | EventType::Error(msg)
| EventType::ErrorSelfNotInGroup(msg) => { | EventType::ErrorSelfNotInGroup(msg) => msg.strdup(),
let data2 = msg.to_c_string().unwrap_or_default();
data2.into_raw()
}
EventType::MsgsChanged { .. } EventType::MsgsChanged { .. }
| EventType::ReactionsChanged { .. } | EventType::ReactionsChanged { .. }
| EventType::IncomingMsg { .. } | EventType::IncomingMsg { .. }
@@ -756,45 +753,27 @@ pub unsafe extern "C" fn dc_event_get_data2_str(event: *mut dc_event_t) -> *mut
| EventType::TransportsModified => ptr::null_mut(), | EventType::TransportsModified => ptr::null_mut(),
EventType::IncomingCall { EventType::IncomingCall {
place_call_info, .. place_call_info, ..
} => { } => place_call_info.strdup(),
let data2 = place_call_info.to_c_string().unwrap_or_default();
data2.into_raw()
}
EventType::OutgoingCallAccepted { EventType::OutgoingCallAccepted {
accept_call_info, .. accept_call_info, ..
} => { } => accept_call_info.strdup(),
let data2 = accept_call_info.to_c_string().unwrap_or_default();
data2.into_raw()
}
EventType::CallEnded { .. } | EventType::EventChannelOverflow { .. } => ptr::null_mut(), EventType::CallEnded { .. } | EventType::EventChannelOverflow { .. } => ptr::null_mut(),
EventType::ConfigureProgress { comment, .. } => { EventType::ConfigureProgress { comment, .. } => {
if let Some(comment) = comment { if let Some(comment) = comment {
comment.to_c_string().unwrap_or_default().into_raw() comment.strdup()
} else { } else {
ptr::null_mut() ptr::null_mut()
} }
} }
EventType::ImexFileWritten(file) => { EventType::ImexFileWritten(file) => file.strdup(),
let data2 = file.to_c_string().unwrap_or_default(); EventType::ConfigSynced { key } => key.to_string().strdup(),
data2.into_raw()
}
EventType::ConfigSynced { key } => {
let data2 = key.to_string().to_c_string().unwrap_or_default();
data2.into_raw()
}
EventType::WebxdcRealtimeData { data, .. } => { EventType::WebxdcRealtimeData { data, .. } => {
let ptr = unsafe { libc::malloc(data.len()) }; let ptr = unsafe { libc::malloc(data.len()) };
unsafe { libc::memcpy(ptr, data.as_ptr() as *mut libc::c_void, data.len()) }; unsafe { libc::memcpy(ptr, data.as_ptr() as *mut libc::c_void, data.len()) };
ptr as *mut libc::c_char ptr as *mut libc::c_char
} }
EventType::IncomingReaction { reaction, .. } => reaction EventType::IncomingReaction { reaction, .. } => reaction.as_str().strdup(),
.as_str() EventType::IncomingWebxdcNotify { text, .. } => text.strdup(),
.to_c_string()
.unwrap_or_default()
.into_raw(),
EventType::IncomingWebxdcNotify { text, .. } => {
text.to_c_string().unwrap_or_default().into_raw()
}
#[allow(unreachable_patterns)] #[allow(unreachable_patterns)]
#[cfg(test)] #[cfg(test)]
_ => unreachable!("This is just to silence a rust_analyzer false-positive"), _ => unreachable!("This is just to silence a rust_analyzer false-positive"),