diff --git a/secp256k1-sys/src/lib.rs b/secp256k1-sys/src/lib.rs index 6239d0b..ddc55e8 100644 --- a/secp256k1-sys/src/lib.rs +++ b/secp256k1-sys/src/lib.rs @@ -798,6 +798,9 @@ pub unsafe extern "C" fn rustsecp256k1_v0_6_1_context_create(flags: c_uint) -> * let bytes = secp256k1_context_preallocated_size(flags) + ALIGN_TO; let layout = alloc::Layout::from_size_align(bytes, ALIGN_TO).unwrap(); let ptr = alloc::alloc(layout); + if ptr.is_null() { + alloc::handle_alloc_error(layout); + } (ptr as *mut usize).write(bytes); // We must offset a whole ALIGN_TO in order to preserve the same alignment // this means we "lose" ALIGN_TO-size_of(usize) for padding. diff --git a/src/context.rs b/src/context.rs index 937ae5d..5a29144 100644 --- a/src/context.rs +++ b/src/context.rs @@ -194,6 +194,9 @@ mod alloc_only { let size = unsafe { ffi::secp256k1_context_preallocated_size(C::FLAGS) }; let layout = alloc::Layout::from_size_align(size, ALIGN_TO).unwrap(); let ptr = unsafe { alloc::alloc(layout) }; + if ptr.is_null() { + alloc::handle_alloc_error(layout); + } #[allow(unused_mut)] // ctx is not mutated under some feature combinations. let mut ctx = Secp256k1 { @@ -254,6 +257,9 @@ mod alloc_only { let size = unsafe { ffi::secp256k1_context_preallocated_clone_size(self.ctx as _) }; let layout = alloc::Layout::from_size_align(size, ALIGN_TO).unwrap(); let ptr = unsafe { alloc::alloc(layout) }; + if ptr.is_null() { + alloc::handle_alloc_error(layout); + } Secp256k1 { ctx: unsafe { ffi::secp256k1_context_preallocated_clone(self.ctx, ptr as *mut c_void)