mirror of
https://github.com/chatmail/core.git
synced 2026-05-02 12:56:30 +03:00
Reduce the scope of unsafe blocks in FFI
Spawn tasks from safe functions to ensure there is no use-after-free.
This commit is contained in:
@@ -421,6 +421,10 @@ pub unsafe extern "C" fn dc_get_oauth2_url(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn spawn_configure(ctx: Context) {
|
||||||
|
spawn(async move { ctx.configure().await.log_err(&ctx, "Configure failed") });
|
||||||
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn dc_configure(context: *mut dc_context_t) {
|
pub unsafe extern "C" fn dc_configure(context: *mut dc_context_t) {
|
||||||
if context.is_null() {
|
if context.is_null() {
|
||||||
@@ -428,10 +432,8 @@ pub unsafe extern "C" fn dc_configure(context: *mut dc_context_t) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clone the context Arc so we do not use the reference after dc_configure() returns.
|
let ctx = &*context;
|
||||||
let ctx = (*context).clone();
|
spawn_configure(ctx.clone());
|
||||||
|
|
||||||
spawn(async move { ctx.configure().await.log_err(&ctx, "Configure failed") });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
@@ -2177,6 +2179,14 @@ pub unsafe extern "C" fn dc_get_contact(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn spawn_imex(ctx: Context, what: imex::ImexMode, param1: String, passphrase: Option<String>) {
|
||||||
|
spawn(async move {
|
||||||
|
imex::imex(&ctx, what, param1.as_ref(), passphrase)
|
||||||
|
.await
|
||||||
|
.log_err(&ctx, "IMEX failed")
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn dc_imex(
|
pub unsafe extern "C" fn dc_imex(
|
||||||
context: *mut dc_context_t,
|
context: *mut dc_context_t,
|
||||||
@@ -2197,15 +2207,10 @@ pub unsafe extern "C" fn dc_imex(
|
|||||||
};
|
};
|
||||||
let passphrase = to_opt_string_lossy(param2);
|
let passphrase = to_opt_string_lossy(param2);
|
||||||
|
|
||||||
// Clone the context Arc so we do not use the reference after dc_imex() returns.
|
let ctx = &*context;
|
||||||
let ctx = (*context).clone();
|
|
||||||
|
|
||||||
if let Some(param1) = to_opt_string_lossy(param1) {
|
if let Some(param1) = to_opt_string_lossy(param1) {
|
||||||
spawn(async move {
|
spawn_imex(ctx.clone(), what, param1, passphrase);
|
||||||
imex::imex(&ctx, what, param1.as_ref(), passphrase)
|
|
||||||
.await
|
|
||||||
.log_err(&ctx, "IMEX failed")
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
eprintln!("dc_imex called without a valid directory");
|
eprintln!("dc_imex called without a valid directory");
|
||||||
}
|
}
|
||||||
@@ -4643,6 +4648,12 @@ mod jsonrpc {
|
|||||||
drop(Box::from_raw(jsonrpc_instance));
|
drop(Box::from_raw(jsonrpc_instance));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn spawn_handle_jsonrpc_request(handle: RpcSession<CommandApi>, request: String) {
|
||||||
|
spawn(async move {
|
||||||
|
handle.handle_incoming(&request).await;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn dc_jsonrpc_request(
|
pub unsafe extern "C" fn dc_jsonrpc_request(
|
||||||
jsonrpc_instance: *mut dc_jsonrpc_instance_t,
|
jsonrpc_instance: *mut dc_jsonrpc_instance_t,
|
||||||
@@ -4654,15 +4665,8 @@ mod jsonrpc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let handle = &(*jsonrpc_instance).handle;
|
let handle = &(*jsonrpc_instance).handle;
|
||||||
|
|
||||||
// Clone the handle so we do not use the reference
|
|
||||||
// in spawned task after return from dc_jsonrpc_request().
|
|
||||||
let handle = handle.clone();
|
|
||||||
|
|
||||||
let request = to_string_lossy(request);
|
let request = to_string_lossy(request);
|
||||||
spawn(async move {
|
spawn_handle_jsonrpc_request(handle.clone(), request);
|
||||||
handle.handle_incoming(&request).await;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
|||||||
Reference in New Issue
Block a user