From 67a5dbdd935d284a406e898d074b2cc820a98508 Mon Sep 17 00:00:00 2001 From: Andrew Hlynskyi Date: Sun, 3 Sep 2023 05:52:24 +0300 Subject: [PATCH] fix: dealloc calls on zero pointers --- cli/src/tests/helpers/allocations.rs | 4 +++- lib/binding_rust/util.rs | 4 +++- lib/src/array.h | 10 ++++++---- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/cli/src/tests/helpers/allocations.rs b/cli/src/tests/helpers/allocations.rs index 2ab80291..0d2331d3 100644 --- a/cli/src/tests/helpers/allocations.rs +++ b/cli/src/tests/helpers/allocations.rs @@ -107,7 +107,9 @@ 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); + if !ptr.is_null() { + record_dealloc(ptr); + } let result = realloc(ptr, size); 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,