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:
parent
13d0ce075e
commit
3a1a280d5f
1 changed files with 4 additions and 5 deletions
|
@ -47,28 +47,27 @@ impl<T> PersistedPerInstanceVec<T> {
|
||||||
f(&mut []);
|
f(&mut []);
|
||||||
} else {
|
} else {
|
||||||
let inner = self.inner.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) });
|
||||||
f(temp.as_mut_slice());
|
f(temp.as_mut_slice());
|
||||||
// Inner Vec has been reallocated, so we need to update the pointer
|
// Inner Vec has been reallocated, so we need to update the pointer
|
||||||
if temp.as_mut_ptr() != inner {
|
if temp.as_mut_ptr() != inner {
|
||||||
self.inner.store(temp.as_mut_ptr(), Ordering::Relaxed);
|
self.inner.store(temp.as_mut_ptr(), Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
self.length.store(temp.len(), Ordering::Relaxed);
|
self.length.store(temp.len(), Ordering::Relaxed);
|
||||||
std::mem::forget(temp);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn push(&self, item: T) {
|
fn push(&self, item: T) {
|
||||||
let length = self.length.load(Ordering::Relaxed);
|
let length = self.length.load(Ordering::Relaxed);
|
||||||
let inner = self.inner.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);
|
temp.push(item);
|
||||||
// Inner Vec has been reallocated, so we need to update the pointer
|
// Inner Vec has been reallocated, so we need to update the pointer
|
||||||
if temp.as_mut_ptr() != inner {
|
if temp.as_mut_ptr() != inner {
|
||||||
self.inner.store(temp.as_mut_ptr(), Ordering::Relaxed);
|
self.inner.store(temp.as_mut_ptr(), Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
std::mem::forget(temp);
|
|
||||||
|
|
||||||
self.length.fetch_add(1, Ordering::Relaxed);
|
self.length.fetch_add(1, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue