From 67a5dbdd935d284a406e898d074b2cc820a98508 Mon Sep 17 00:00:00 2001 From: Andrew Hlynskyi Date: Sun, 3 Sep 2023 05:52:24 +0300 Subject: [PATCH 1/2] 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, From 7f7084c2cb64a1617746ac6c7bbdb773131593a7 Mon Sep 17 00:00:00 2001 From: Andrew Hlynskyi Date: Sun, 3 Sep 2023 05:29:48 +0300 Subject: [PATCH 2/2] chore(test): panic on zero pointer deallocs for alloc tracked scopes --- cli/src/tests/helpers/allocations.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cli/src/tests/helpers/allocations.rs b/cli/src/tests/helpers/allocations.rs index 0d2331d3..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,11 +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 { - if !ptr.is_null() { - 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 }