Make Context::deallocate unsafe fn

This commit is contained in:
Elichai Turkel 2019-11-27 17:42:01 +02:00
parent fe688ada65
commit 9522f7e4a4
No known key found for this signature in database
GPG Key ID: 9383CDE9E8E66A7F
2 changed files with 14 additions and 13 deletions

View File

@ -15,7 +15,7 @@ pub unsafe trait Context : private::Sealed {
/// A constant description of the context. /// A constant description of the context.
const DESCRIPTION: &'static str; const DESCRIPTION: &'static str;
/// A function to deallocate the memory when the context is dropped. /// A function to deallocate the memory when the context is dropped.
fn deallocate(ptr: *mut [u8]); unsafe fn deallocate(ptr: *mut [u8]);
} }
/// Marker trait for indicating that an instance of `Secp256k1` can be used for signing. /// Marker trait for indicating that an instance of `Secp256k1` can be used for signing.
@ -78,8 +78,8 @@ mod std_only {
const FLAGS: c_uint = ffi::SECP256K1_START_SIGN; const FLAGS: c_uint = ffi::SECP256K1_START_SIGN;
const DESCRIPTION: &'static str = "signing only"; const DESCRIPTION: &'static str = "signing only";
fn deallocate(ptr: *mut [u8]) { unsafe fn deallocate(ptr: *mut [u8]) {
let _ = unsafe { Box::from_raw(ptr) }; let _ = Box::from_raw(ptr);
} }
} }
@ -87,8 +87,8 @@ mod std_only {
const FLAGS: c_uint = ffi::SECP256K1_START_VERIFY; const FLAGS: c_uint = ffi::SECP256K1_START_VERIFY;
const DESCRIPTION: &'static str = "verification only"; const DESCRIPTION: &'static str = "verification only";
fn deallocate(ptr: *mut [u8]) { unsafe fn deallocate(ptr: *mut [u8]) {
let _ = unsafe { Box::from_raw(ptr) }; let _ = Box::from_raw(ptr);
} }
} }
@ -96,8 +96,8 @@ mod std_only {
const FLAGS: c_uint = VerifyOnly::FLAGS | SignOnly::FLAGS; const FLAGS: c_uint = VerifyOnly::FLAGS | SignOnly::FLAGS;
const DESCRIPTION: &'static str = "all capabilities"; const DESCRIPTION: &'static str = "all capabilities";
fn deallocate(ptr: *mut [u8]) { unsafe fn deallocate(ptr: *mut [u8]) {
let _ = unsafe { Box::from_raw(ptr) }; let _ = Box::from_raw(ptr);
} }
} }
@ -152,7 +152,6 @@ mod std_only {
} }
} }
} }
} }
impl<'buf> Signing for SignOnlyPreallocated<'buf> {} impl<'buf> Signing for SignOnlyPreallocated<'buf> {}
@ -165,7 +164,7 @@ unsafe impl<'buf> Context for SignOnlyPreallocated<'buf> {
const FLAGS: c_uint = ffi::SECP256K1_START_SIGN; const FLAGS: c_uint = ffi::SECP256K1_START_SIGN;
const DESCRIPTION: &'static str = "signing only"; const DESCRIPTION: &'static str = "signing only";
fn deallocate(_ptr: *mut [u8]) { unsafe fn deallocate(_ptr: *mut [u8]) {
// Allocated by the user // Allocated by the user
} }
} }
@ -174,7 +173,7 @@ unsafe impl<'buf> Context for VerifyOnlyPreallocated<'buf> {
const FLAGS: c_uint = ffi::SECP256K1_START_VERIFY; const FLAGS: c_uint = ffi::SECP256K1_START_VERIFY;
const DESCRIPTION: &'static str = "verification only"; const DESCRIPTION: &'static str = "verification only";
fn deallocate(_ptr: *mut [u8]) { unsafe fn deallocate(_ptr: *mut [u8]) {
// Allocated by the user // Allocated by the user
} }
} }
@ -183,7 +182,7 @@ unsafe impl<'buf> Context for AllPreallocated<'buf> {
const FLAGS: c_uint = SignOnlyPreallocated::FLAGS | VerifyOnlyPreallocated::FLAGS; const FLAGS: c_uint = SignOnlyPreallocated::FLAGS | VerifyOnlyPreallocated::FLAGS;
const DESCRIPTION: &'static str = "all capabilities"; const DESCRIPTION: &'static str = "all capabilities";
fn deallocate(_ptr: *mut [u8]) { unsafe fn deallocate(_ptr: *mut [u8]) {
// Allocated by the user // Allocated by the user
} }
} }

View File

@ -571,8 +571,10 @@ impl<C: Context> Eq for Secp256k1<C> { }
impl<C: Context> Drop for Secp256k1<C> { impl<C: Context> Drop for Secp256k1<C> {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { ffi::secp256k1_context_preallocated_destroy(self.ctx) }; unsafe {
C::deallocate(self.buf); ffi::secp256k1_context_preallocated_destroy(self.ctx);
C::deallocate(self.buf);
}
} }
} }