src: store pointer to Environment on DestroyParam

To avoid a potential segfault when inside WeakCallback, store a
reference to Environment inside DestroyParam.

PR-URL: https://github.com/nodejs/node/pull/21099
Reviewed-By: Yang Guo <yangguo@chromium.org>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>

Co-authored-by: Yang Guo <yangguo@chromium.org>
Co-authored-by: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
Anatoli Papirovski 2018-06-02 18:10:34 +02:00
parent 3c5b8b4602
commit 0300f7c68c
No known key found for this signature in database
GPG Key ID: 614E2E1ABEB4B2C0

View File

@ -398,6 +398,7 @@ static void DisablePromiseHook(const FunctionCallbackInfo<Value>& args) {
class DestroyParam {
public:
double asyncId;
Environment* env;
Persistent<Object> target;
Persistent<Object> propBag;
};
@ -406,13 +407,12 @@ class DestroyParam {
void AsyncWrap::WeakCallback(const v8::WeakCallbackInfo<DestroyParam>& info) {
HandleScope scope(info.GetIsolate());
Environment* env = Environment::GetCurrent(info.GetIsolate());
std::unique_ptr<DestroyParam> p{info.GetParameter()};
Local<Object> prop_bag = PersistentToLocal(info.GetIsolate(), p->propBag);
Local<Value> val = prop_bag->Get(env->destroyed_string());
Local<Value> val = prop_bag->Get(p->env->destroyed_string());
if (val->IsFalse()) {
AsyncWrap::EmitDestroy(env, p->asyncId);
AsyncWrap::EmitDestroy(p->env, p->asyncId);
}
// unique_ptr goes out of scope here and pointer is deleted.
}
@ -426,6 +426,7 @@ static void RegisterDestroyHook(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
DestroyParam* p = new DestroyParam();
p->asyncId = args[1].As<Number>()->Value();
p->env = Environment::GetCurrent(args);
p->target.Reset(isolate, args[0].As<Object>());
p->propBag.Reset(isolate, args[2].As<Object>());
p->target.SetWeak(