Skip to content

Commit 8b17fc0

Browse files
committed
call the alloc error handle if we get NULL from the allocator
1 parent fb45ae4 commit 8b17fc0

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

secp256k1-sys/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,9 @@ pub unsafe extern "C" fn rustsecp256k1_v0_6_1_context_create(flags: c_uint) -> *
798798
let bytes = secp256k1_context_preallocated_size(flags) + ALIGN_TO;
799799
let layout = alloc::Layout::from_size_align(bytes, ALIGN_TO).unwrap();
800800
let ptr = alloc::alloc(layout);
801+
if ptr.is_null() {
802+
alloc::handle_alloc_error(layout);
803+
}
801804
(ptr as *mut usize).write(bytes);
802805
// We must offset a whole ALIGN_TO in order to preserve the same alignment
803806
// this means we "lose" ALIGN_TO-size_of(usize) for padding.

src/context.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ mod alloc_only {
194194
let size = unsafe { ffi::secp256k1_context_preallocated_size(C::FLAGS) };
195195
let layout = alloc::Layout::from_size_align(size, ALIGN_TO).unwrap();
196196
let ptr = unsafe { alloc::alloc(layout) };
197+
if ptr.is_null() {
198+
alloc::handle_alloc_error(layout);
199+
}
197200

198201
#[allow(unused_mut)] // ctx is not mutated under some feature combinations.
199202
let mut ctx = Secp256k1 {
@@ -254,6 +257,9 @@ mod alloc_only {
254257
let size = unsafe { ffi::secp256k1_context_preallocated_clone_size(self.ctx as _) };
255258
let layout = alloc::Layout::from_size_align(size, ALIGN_TO).unwrap();
256259
let ptr = unsafe { alloc::alloc(layout) };
260+
if ptr.is_null() {
261+
alloc::handle_alloc_error(layout);
262+
}
257263
Secp256k1 {
258264
ctx: unsafe {
259265
ffi::secp256k1_context_preallocated_clone(self.ctx, ptr as *mut c_void)

0 commit comments

Comments
 (0)