Skip to content

Commit 8ab0bbc

Browse files
committed
Merge #535: call the alloc error handle if we get NULL from the allocator
8b17fc0 call the alloc error handle if we get NULL from the allocator (Elichai Turkel) Pull request description: Found that this was missing in this discussion: #529 (comment) It is documented here that it returns a NULL on memory exhaustion: https://doc.rust-lang.org/alloc/alloc/trait.GlobalAlloc.html#tymethod.alloc And you can see that this is called in this example: https://doc.rust-lang.org/alloc/alloc/fn.alloc.html Docs for the handle itself: https://doc.rust-lang.org/alloc/alloc/fn.handle_alloc_error.html ACKs for top commit: apoelstra: ACK 8b17fc0 Kixunil: Good argument, ACK 8b17fc0 Tree-SHA512: 4b8f79ab5f691cb92621a314ceb8556c26fa7e159de359697b766043a0269e1ecf9746e6d4bfd5b45f18bccaff435c1fff491168b8bb77459ae849c38664d563
2 parents e4baf79 + 8b17fc0 commit 8ab0bbc

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

secp256k1-sys/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,9 @@ pub unsafe extern "C" fn rustsecp256k1_v0_6_1_context_create(flags: c_uint) -> *
811811
let bytes = secp256k1_context_preallocated_size(flags) + ALIGN_TO;
812812
let layout = alloc::Layout::from_size_align(bytes, ALIGN_TO).unwrap();
813813
let ptr = alloc::alloc(layout);
814+
if ptr.is_null() {
815+
alloc::handle_alloc_error(layout);
816+
}
814817
(ptr as *mut usize).write(bytes);
815818
// We must offset a whole ALIGN_TO in order to preserve the same alignment
816819
// this means we "lose" ALIGN_TO-size_of(usize) for padding.

src/context.rs

+6
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ mod alloc_only {
202202
let size = unsafe { ffi::secp256k1_context_preallocated_size(C::FLAGS) };
203203
let layout = alloc::Layout::from_size_align(size, ALIGN_TO).unwrap();
204204
let ptr = unsafe { alloc::alloc(layout) };
205+
if ptr.is_null() {
206+
alloc::handle_alloc_error(layout);
207+
}
205208

206209
#[allow(unused_mut)] // ctx is not mutated under some feature combinations.
207210
let mut ctx = Secp256k1 {
@@ -262,6 +265,9 @@ mod alloc_only {
262265
let size = unsafe { ffi::secp256k1_context_preallocated_clone_size(self.ctx as _) };
263266
let layout = alloc::Layout::from_size_align(size, ALIGN_TO).unwrap();
264267
let ptr = unsafe { alloc::alloc(layout) };
268+
if ptr.is_null() {
269+
alloc::handle_alloc_error(layout);
270+
}
265271
Secp256k1 {
266272
ctx: unsafe {
267273
ffi::secp256k1_context_preallocated_clone(self.ctx, ptr as *mut c_void)

0 commit comments

Comments
 (0)