From 0ba8201797ff05fba62269caa7fecea988a02125 Mon Sep 17 00:00:00 2001 From: Floris Bruynooghe Date: Wed, 22 Mar 2023 17:34:00 +0100 Subject: [PATCH 1/3] fix(dc_receive_backup): Increase refcount before spawn Otherwise it is possible for the context that is used in the spawn to be unreferenced. Really this should be caught by the borrow checker that ensures we only spawn things with a 'static lifetime, but we're handling raw pointers so it doesn't. --- deltachat-ffi/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 09ec1a805..d9f445813 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -4248,10 +4248,11 @@ pub unsafe extern "C" fn dc_receive_backup( Ok(qr) => qr, Err(_) => return 0, }; + let ctx = ctx.clone(); spawn(async move { - imex::get_backup(ctx, qr) + imex::get_backup(&ctx, qr) .await - .log_err(ctx, "Get backup failed") + .log_err(&ctx, "Get backup failed") .ok(); }); 1 From c42d9424600201b65266b78c1a2321b1d1134c39 Mon Sep 17 00:00:00 2001 From: Floris Bruynooghe Date: Wed, 22 Mar 2023 17:37:24 +0100 Subject: [PATCH 2/3] explicitly move for good measure --- deltachat-ffi/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index d9f445813..5e145d16b 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -4250,6 +4250,7 @@ pub unsafe extern "C" fn dc_receive_backup( }; let ctx = ctx.clone(); spawn(async move { + let ctx = ctx; imex::get_backup(&ctx, qr) .await .log_err(&ctx, "Get backup failed") From f033aae25c8c953694c9fec20ee88e42f7aa54ed Mon Sep 17 00:00:00 2001 From: link2xt Date: Thu, 23 Mar 2023 08:47:33 +0000 Subject: [PATCH 3/3] Move most of the `dc_receive_backup()` into a safe function --- deltachat-ffi/src/lib.rs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 5e145d16b..e0266e925 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -4233,6 +4233,21 @@ pub unsafe extern "C" fn dc_backup_provider_unref(provider: *mut dc_backup_provi drop(Box::from_raw(provider)); } +fn receive_backup(ctx: Context, qr_text: String) -> libc::c_int { + let qr = match block_on(qr::check_qr(&ctx, &qr_text)).log_err(&ctx, "Invalid QR code") { + Ok(qr) => qr, + Err(_) => return 0, + }; + spawn(async move { + let ctx = ctx; + imex::get_backup(&ctx, qr) + .await + .log_err(&ctx, "Get backup failed") + .ok(); + }); + 1 +} + #[no_mangle] pub unsafe extern "C" fn dc_receive_backup( context: *mut dc_context_t, @@ -4244,19 +4259,7 @@ pub unsafe extern "C" fn dc_receive_backup( } let ctx = &*context; let qr_text = to_string_lossy(qr); - let qr = match block_on(qr::check_qr(ctx, &qr_text)).log_err(ctx, "Invalid QR code") { - Ok(qr) => qr, - Err(_) => return 0, - }; - let ctx = ctx.clone(); - spawn(async move { - let ctx = ctx; - imex::get_backup(&ctx, qr) - .await - .log_err(&ctx, "Get backup failed") - .ok(); - }); - 1 + receive_backup(ctx.clone(), qr_text) } trait ResultExt {