diff --git a/secp256k1-sys/src/lib.rs b/secp256k1-sys/src/lib.rs index 496d1bc..243c941 100644 --- a/secp256k1-sys/src/lib.rs +++ b/secp256k1-sys/src/lib.rs @@ -811,6 +811,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 f69be99..52402d0 100644 --- a/src/context.rs +++ b/src/context.rs @@ -202,6 +202,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 { @@ -262,6 +265,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)