Merge pull request #579 from napi-rs/fix-memory-alloc

fix(napi): wrong memory location of data in future resolved callback
This commit is contained in:
LongYinan 2021-05-27 23:21:07 +08:00 committed by GitHub
commit 185074f11d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -58,7 +58,7 @@ impl<T, V: NapiRaw> FuturePromise<T, V> {
1, 1,
ptr::null_mut(), ptr::null_mut(),
None, None,
self_ref as *mut _ as *mut c_void, self_ref as *mut FuturePromise<T, V> as *mut c_void,
Some(call_js_cb::<T, V>), Some(call_js_cb::<T, V>),
&mut tsfn_value, &mut tsfn_value,
) )
@ -81,7 +81,7 @@ pub(crate) async fn resolve_from_future<T: Send, F: Future<Output = Result<T>>>(
check_status!(unsafe { check_status!(unsafe {
sys::napi_call_threadsafe_function( sys::napi_call_threadsafe_function(
tsfn_value.0, tsfn_value.0,
Box::into_raw(Box::from(val)) as *mut _ as *mut c_void, Box::into_raw(Box::from(val)) as *mut T as *mut c_void,
sys::napi_threadsafe_function_call_mode::napi_tsfn_nonblocking, sys::napi_threadsafe_function_call_mode::napi_tsfn_nonblocking,
) )
}) })
@ -103,7 +103,7 @@ unsafe extern "C" fn call_js_cb<T, V: NapiRaw>(
) { ) {
let mut env = Env::from_raw(raw_env); let mut env = Env::from_raw(raw_env);
let future_promise = Box::from_raw(context as *mut FuturePromise<T, V>); let future_promise = Box::from_raw(context as *mut FuturePromise<T, V>);
let value: Result<T> = ptr::read(data as *const _); let value = Box::from_raw(data as *mut Result<T>);
let resolver = future_promise.resolver; let resolver = future_promise.resolver;
let deferred = future_promise.deferred; let deferred = future_promise.deferred;
let js_value_to_resolve = value.and_then(move |v| (resolver)(&mut env, v)); let js_value_to_resolve = value.and_then(move |v| (resolver)(&mut env, v));