diff --git a/cli/src/tests/helpers/allocations.rs b/cli/src/tests/helpers/allocations.rs index 2ab80291..43537633 100644 --- a/cli/src/tests/helpers/allocations.rs +++ b/cli/src/tests/helpers/allocations.rs @@ -83,6 +83,9 @@ fn record_alloc(ptr: *mut c_void) { } fn record_dealloc(ptr: *mut c_void) { + if ptr.is_null() { + panic!("Zero pointer deallocation!"); + } RECORDER.with(|recorder| { if recorder.enabled.load(SeqCst) { recorder @@ -107,9 +110,13 @@ unsafe extern "C" fn ts_record_calloc(count: usize, size: usize) -> *mut c_void } unsafe extern "C" fn ts_record_realloc(ptr: *mut c_void, size: usize) -> *mut c_void { - record_dealloc(ptr); let result = realloc(ptr, size); - record_alloc(result); + if ptr.is_null() { + record_alloc(result); + } else if ptr != result { + record_dealloc(ptr); + record_alloc(result); + } result } diff --git a/lib/binding_rust/util.rs b/lib/binding_rust/util.rs index 5eda71f4..d5a73437 100644 --- a/lib/binding_rust/util.rs +++ b/lib/binding_rust/util.rs @@ -37,6 +37,8 @@ impl ExactSizeIterator for CBufferIter {} impl Drop for CBufferIter { fn drop(&mut self) { - unsafe { (FREE_FN)(self.ptr as *mut c_void) }; + if !self.ptr.is_null() { + unsafe { (FREE_FN)(self.ptr as *mut c_void) }; + } } } diff --git a/lib/src/array.h b/lib/src/array.h index e5cd361f..e026f6b2 100644 --- a/lib/src/array.h +++ b/lib/src/array.h @@ -132,10 +132,12 @@ typedef Array(void) VoidArray; #define array__elem_size(self) sizeof(*(self)->contents) static inline void array__delete(VoidArray *self) { - ts_free(self->contents); - self->contents = NULL; - self->size = 0; - self->capacity = 0; + if (self->contents) { + ts_free(self->contents); + self->contents = NULL; + self->size = 0; + self->capacity = 0; + } } static inline void array__erase(VoidArray *self, size_t element_size,