fix(napi): potential double free issue (#1679)

* Update module_register.rs

* Run fmt

---------

Co-authored-by: LongYinan <lynweklm@gmail.com>
This commit is contained in:
kuzeyardabulut 2023-11-02 12:19:35 +03:00 committed by GitHub
parent 13d0ce075e
commit 3a1a280d5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -47,28 +47,27 @@ impl<T> PersistedPerInstanceVec<T> {
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);
}
}