From 9831d0e397f8c1c34de84591b5e02634e44f229e Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Sat, 22 Oct 2022 23:13:02 +0100 Subject: [PATCH] Check that allocation does not exceed isize::MAX See https://github.com/rust-lang/rust/issues/101899 --- src/lua.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/lua.rs b/src/lua.rs index 838c107..6571f97 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -402,6 +402,11 @@ impl Lua { return ptr::null_mut(); } + // Do not allocate more than isize::MAX + if nsize > isize::MAX as usize { + return ptr::null_mut(); + } + // Are we fit to the memory limits? let mut mem_diff = nsize as isize; if !ptr.is_null() { @@ -411,12 +416,14 @@ impl Lua { if mem_info.memory_limit > 0 && new_used_memory > mem_info.memory_limit { return ptr::null_mut(); } - - let new_layout = Layout::from_size_align_unchecked(nsize, ffi::SYS_MIN_ALIGN); mem_info.used_memory += mem_diff; if ptr.is_null() { // Allocate new memory + let new_layout = match Layout::from_size_align(nsize, ffi::SYS_MIN_ALIGN) { + Ok(layout) => layout, + Err(_) => return ptr::null_mut(), + }; let new_ptr = alloc::alloc(new_layout) as *mut c_void; if new_ptr.is_null() { alloc::handle_alloc_error(new_layout); @@ -428,7 +435,7 @@ impl Lua { let old_layout = Layout::from_size_align_unchecked(osize, ffi::SYS_MIN_ALIGN); let new_ptr = alloc::realloc(ptr as *mut u8, old_layout, nsize) as *mut c_void; if new_ptr.is_null() { - alloc::handle_alloc_error(new_layout); + alloc::handle_alloc_error(old_layout); } new_ptr }