Skip to content

Commit 67a5dbd

Browse files
committed
fix: dealloc calls on zero pointers
1 parent 5921b36 commit 67a5dbd

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

cli/src/tests/helpers/allocations.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ unsafe extern "C" fn ts_record_calloc(count: usize, size: usize) -> *mut c_void
107107
}
108108

109109
unsafe extern "C" fn ts_record_realloc(ptr: *mut c_void, size: usize) -> *mut c_void {
110-
record_dealloc(ptr);
110+
if !ptr.is_null() {
111+
record_dealloc(ptr);
112+
}
111113
let result = realloc(ptr, size);
112114
record_alloc(result);
113115
result

lib/binding_rust/util.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ impl<T: Copy> ExactSizeIterator for CBufferIter<T> {}
3737

3838
impl<T> Drop for CBufferIter<T> {
3939
fn drop(&mut self) {
40-
unsafe { (FREE_FN)(self.ptr as *mut c_void) };
40+
if !self.ptr.is_null() {
41+
unsafe { (FREE_FN)(self.ptr as *mut c_void) };
42+
}
4143
}
4244
}

lib/src/array.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,12 @@ typedef Array(void) VoidArray;
132132
#define array__elem_size(self) sizeof(*(self)->contents)
133133

134134
static inline void array__delete(VoidArray *self) {
135-
ts_free(self->contents);
136-
self->contents = NULL;
137-
self->size = 0;
138-
self->capacity = 0;
135+
if (self->contents) {
136+
ts_free(self->contents);
137+
self->contents = NULL;
138+
self->size = 0;
139+
self->capacity = 0;
140+
}
139141
}
140142

141143
static inline void array__erase(VoidArray *self, size_t element_size,

0 commit comments

Comments
 (0)