From ab18eb0776002edef1c5a736948037fdc8914b66 Mon Sep 17 00:00:00 2001 From: kelas Date: Thu, 9 Jun 2022 23:01:43 +0200 Subject: [PATCH] Fix realloc() --- services/ffi-test/src/bindings.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/services/ffi-test/src/bindings.rs b/services/ffi-test/src/bindings.rs index 7bfb0183a..689f69277 100644 --- a/services/ffi-test/src/bindings.rs +++ b/services/ffi-test/src/bindings.rs @@ -148,6 +148,10 @@ pub unsafe extern "C" fn realloc( ptr: *mut c_void, size: c_uint ) -> *mut c_void { + if ptr.is_null() { + // if ptr is null, realloc() is identical to malloc() + return malloc(size); + } let mut region_index: Option = None; for (index, region) in C_HEAP.iter().enumerate() { if region.as_ptr() as usize == ptr as usize { @@ -158,7 +162,7 @@ pub unsafe extern "C" fn realloc( match region_index { Some(index) => { log::info!("realloc/free success: {:x}", ptr as usize); - let mut old = C_HEAP.remove(index); + let mut old = C_HEAP.swap_remove(index); let checked_size = if size == 0 { 1 // at least 1 element so we have a pointer we can pass back } else { @@ -170,6 +174,7 @@ pub unsafe extern "C" fn realloc( alloc.push(src); } old.clear(); + alloc.set_len(checked_size as usize); C_HEAP.push(alloc); log::info!("realloc/allocated: {:x}", ret_ptr as usize);