From 3a1a280d5f89d1ae7d429dfadd79410b657d3f39 Mon Sep 17 00:00:00 2001 From: kuzeyardabulut <54737933+kuzeyardabulut@users.noreply.github.com> Date: Thu, 2 Nov 2023 12:19:35 +0300 Subject: [PATCH] fix(napi): potential double free issue (#1679) * Update module_register.rs * Run fmt --------- Co-authored-by: LongYinan --- crates/napi/src/bindgen_runtime/module_register.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/napi/src/bindgen_runtime/module_register.rs b/crates/napi/src/bindgen_runtime/module_register.rs index aa4f3e8c..57c44861 100644 --- a/crates/napi/src/bindgen_runtime/module_register.rs +++ b/crates/napi/src/bindgen_runtime/module_register.rs @@ -47,28 +47,27 @@ impl PersistedPerInstanceVec { f(&mut []); } else { let inner = self.inner.load(Ordering::Relaxed); - let mut temp = unsafe { Vec::from_raw_parts(inner, length, length) }; + let mut temp = + std::mem::ManuallyDrop::new(unsafe { Vec::from_raw_parts(inner, length, length) }); f(temp.as_mut_slice()); // Inner Vec has been reallocated, so we need to update the pointer if temp.as_mut_ptr() != inner { self.inner.store(temp.as_mut_ptr(), Ordering::Relaxed); } self.length.store(temp.len(), Ordering::Relaxed); - std::mem::forget(temp); } } fn push(&self, item: T) { let length = self.length.load(Ordering::Relaxed); let inner = self.inner.load(Ordering::Relaxed); - let mut temp = unsafe { Vec::from_raw_parts(inner, length, length) }; + let mut temp = + std::mem::ManuallyDrop::new(unsafe { Vec::from_raw_parts(inner, length, length) }); temp.push(item); // Inner Vec has been reallocated, so we need to update the pointer if temp.as_mut_ptr() != inner { self.inner.store(temp.as_mut_ptr(), Ordering::Relaxed); } - std::mem::forget(temp); - self.length.fetch_add(1, Ordering::Relaxed); } }