From d2c4e5a846a6d6d77212f7f80589ce962a224fa1 Mon Sep 17 00:00:00 2001 From: Elichai Turkel Date: Thu, 5 Dec 2019 17:51:59 +0200 Subject: [PATCH] Wrap Secp256k1 from raw context in a ManuallyDrop --- src/context.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/context.rs b/src/context.rs index 13d341e..a3d8fe8 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,4 +1,5 @@ use core::marker::PhantomData; +use core::mem::ManuallyDrop; use ptr; use ffi::{self, CPtr}; use ffi::types::{c_uint, c_void}; @@ -227,12 +228,12 @@ impl<'buf> Secp256k1> { /// * The user must handle the freeing of the context(using the correct functions) by himself. /// * Violating these may lead to Undefined Behavior. /// - pub unsafe fn from_raw_all(raw_ctx: *mut ffi::Context) -> Secp256k1> { - Secp256k1 { + pub unsafe fn from_raw_all(raw_ctx: *mut ffi::Context) -> ManuallyDrop>> { + ManuallyDrop::new(Secp256k1 { ctx: raw_ctx, phantom: PhantomData, buf: ptr::null_mut::<[u8;0]>() as *mut [u8] , - } + }) } } @@ -259,12 +260,12 @@ impl<'buf> Secp256k1> { /// * The user must handle the freeing of the context(using the correct functions) by himself. /// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior., /// - pub unsafe fn from_raw_signining_only(raw_ctx: *mut ffi::Context) -> Secp256k1> { - Secp256k1 { + pub unsafe fn from_raw_signining_only(raw_ctx: *mut ffi::Context) -> ManuallyDrop>> { + ManuallyDrop::new(Secp256k1 { ctx: raw_ctx, phantom: PhantomData, buf: ptr::null_mut::<[u8;0]>() as *mut [u8] , - } + }) } } @@ -291,11 +292,11 @@ impl<'buf> Secp256k1> { /// * The user must handle the freeing of the context(using the correct functions) by himself. /// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior., /// - pub unsafe fn from_raw_verification_only(raw_ctx: *mut ffi::Context) -> Secp256k1> { - Secp256k1 { + pub unsafe fn from_raw_verification_only(raw_ctx: *mut ffi::Context) -> ManuallyDrop>> { + ManuallyDrop::new(Secp256k1 { ctx: raw_ctx, phantom: PhantomData, buf: ptr::null_mut::<[u8;0]>() as *mut [u8] , - } + }) } } \ No newline at end of file