Skip to content

Commit b5acbaa

Browse files
committedMar 12, 2025
api(ffi): store reference pointer to Context in dc_chat_t
This avoids problems if dc_context_t is unreferenced, invalidating dc_chat_t that was derived from it.
1 parent b5de5d0 commit b5acbaa

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed
 

‎deltachat-ffi/src/lib.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,7 @@ pub unsafe extern "C" fn dc_get_chat(context: *mut dc_context_t, chat_id: u32) -
16581658
return ptr::null_mut();
16591659
}
16601660
let ctx = &*context;
1661+
let context: Context = ctx.clone();
16611662

16621663
block_on(async move {
16631664
match chat::Chat::load_from_db(ctx, ChatId::new(chat_id)).await {
@@ -2982,7 +2983,7 @@ pub unsafe extern "C" fn dc_chatlist_get_context(
29822983
/// context, but the Rust API does not, so the FFI layer needs to glue
29832984
/// these together.
29842985
pub struct ChatWrapper {
2985-
context: *const dc_context_t,
2986+
context: Context,
29862987
chat: chat::Chat,
29872988
}
29882989

@@ -3049,14 +3050,13 @@ pub unsafe extern "C" fn dc_chat_get_profile_image(chat: *mut dc_chat_t) -> *mut
30493050
return ptr::null_mut(); // NULL explicitly defined as "no image"
30503051
}
30513052
let ffi_chat = &*chat;
3052-
let ctx = &*ffi_chat.context;
30533053

30543054
block_on(async move {
3055-
match ffi_chat.chat.get_profile_image(ctx).await {
3055+
match ffi_chat.chat.get_profile_image(&ffi_chat.context).await {
30563056
Ok(Some(p)) => p.to_string_lossy().strdup(),
30573057
Ok(None) => ptr::null_mut(),
30583058
Err(err) => {
3059-
error!(ctx, "failed to get profile image: {err:#}");
3059+
error!(ffi_chat.context, "failed to get profile image: {err:#}");
30603060
ptr::null_mut()
30613061
}
30623062
}
@@ -3070,9 +3070,9 @@ pub unsafe extern "C" fn dc_chat_get_color(chat: *mut dc_chat_t) -> u32 {
30703070
return 0;
30713071
}
30723072
let ffi_chat = &*chat;
3073-
let ctx = &*ffi_chat.context;
30743073

3075-
block_on(ffi_chat.chat.get_color(ctx)).unwrap_or_log_default(ctx, "Failed get_color")
3074+
block_on(ffi_chat.chat.get_color(&ffi_chat.context))
3075+
.unwrap_or_log_default(&ffi_chat.context, "Failed get_color")
30763076
}
30773077

30783078
#[no_mangle]
@@ -3136,10 +3136,9 @@ pub unsafe extern "C" fn dc_chat_can_send(chat: *mut dc_chat_t) -> libc::c_int {
31363136
return 0;
31373137
}
31383138
let ffi_chat = &*chat;
3139-
let ctx = &*ffi_chat.context;
3140-
block_on(ffi_chat.chat.can_send(ctx))
3139+
block_on(ffi_chat.chat.can_send(&ffi_chat.context))
31413140
.context("can_send failed")
3142-
.log_err(ctx)
3141+
.log_err(&ffi_chat.context)
31433142
.unwrap_or_default() as libc::c_int
31443143
}
31453144

0 commit comments

Comments
 (0)