Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
n-api: fix use-after-free with napi_remove_async_cleanup_hook
Fixes: #34657
Refs: #34572
  • Loading branch information
addaleax committed Aug 7, 2020
commit b94ceaf88d9c6662fc53f7991c967203c5007d85
6 changes: 6 additions & 0 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ napi_status napi_add_async_cleanup_hook(
auto handle = node::AddEnvironmentCleanupHook(env->isolate, fun, arg);
if (remove_handle != nullptr) {
*remove_handle = new napi_async_cleanup_hook_handle__ { std::move(handle) };
env->Ref();
}

return napi_clear_last_error(env);
Expand All @@ -547,6 +548,11 @@ napi_status napi_remove_async_cleanup_hook(
node::RemoveEnvironmentCleanupHook(std::move(remove_handle->handle));
delete remove_handle;

// Release the `env` handle asynchronously since it would be surprising if
// a call to a N-API function would destroy `env` synchronously.
static_cast<node_napi_env>(env)->node_env()
->SetImmediate([env](node::Environment*) { env->Unref(); });

return napi_clear_last_error(env);
}

Expand Down