Wrap Secp256k1 from raw context in a ManuallyDrop

This commit is contained in:
Elichai Turkel 2019-12-05 17:51:59 +02:00
parent e3aaf00710
commit d2c4e5a846
No known key found for this signature in database
GPG Key ID: 9383CDE9E8E66A7F
1 changed files with 10 additions and 9 deletions

View File

@ -1,4 +1,5 @@
use core::marker::PhantomData; use core::marker::PhantomData;
use core::mem::ManuallyDrop;
use ptr; use ptr;
use ffi::{self, CPtr}; use ffi::{self, CPtr};
use ffi::types::{c_uint, c_void}; use ffi::types::{c_uint, c_void};
@ -227,12 +228,12 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
/// * The user must handle the freeing of the context(using the correct functions) by himself. /// * The user must handle the freeing of the context(using the correct functions) by himself.
/// * Violating these may lead to Undefined Behavior. /// * Violating these may lead to Undefined Behavior.
/// ///
pub unsafe fn from_raw_all(raw_ctx: *mut ffi::Context) -> Secp256k1<AllPreallocated<'buf>> { pub unsafe fn from_raw_all(raw_ctx: *mut ffi::Context) -> ManuallyDrop<Secp256k1<AllPreallocated<'buf>>> {
Secp256k1 { ManuallyDrop::new(Secp256k1 {
ctx: raw_ctx, ctx: raw_ctx,
phantom: PhantomData, phantom: PhantomData,
buf: ptr::null_mut::<[u8;0]>() as *mut [u8] , buf: ptr::null_mut::<[u8;0]>() as *mut [u8] ,
} })
} }
} }
@ -259,12 +260,12 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
/// * The user must handle the freeing of the context(using the correct functions) by himself. /// * 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., /// * 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<SignOnlyPreallocated<'buf>> { pub unsafe fn from_raw_signining_only(raw_ctx: *mut ffi::Context) -> ManuallyDrop<Secp256k1<SignOnlyPreallocated<'buf>>> {
Secp256k1 { ManuallyDrop::new(Secp256k1 {
ctx: raw_ctx, ctx: raw_ctx,
phantom: PhantomData, phantom: PhantomData,
buf: ptr::null_mut::<[u8;0]>() as *mut [u8] , buf: ptr::null_mut::<[u8;0]>() as *mut [u8] ,
} })
} }
} }
@ -291,11 +292,11 @@ impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
/// * The user must handle the freeing of the context(using the correct functions) by himself. /// * 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., /// * 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<VerifyOnlyPreallocated<'buf>> { pub unsafe fn from_raw_verification_only(raw_ctx: *mut ffi::Context) -> ManuallyDrop<Secp256k1<VerifyOnlyPreallocated<'buf>>> {
Secp256k1 { ManuallyDrop::new(Secp256k1 {
ctx: raw_ctx, ctx: raw_ctx,
phantom: PhantomData, phantom: PhantomData,
buf: ptr::null_mut::<[u8;0]>() as *mut [u8] , buf: ptr::null_mut::<[u8;0]>() as *mut [u8] ,
} })
} }
} }