From 8862f0a613c3da77b6cffa24a4374df83d30c300 Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Sat, 2 Jun 2018 18:10:34 +0200 Subject: [PATCH] src: store pointer to Environment on DestroyParam MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Tiancheng "Timothy" Gu Co-authored-by: Yang Guo Co-authored-by: Michaƫl Zasso --- src/async_wrap.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/async_wrap.cc b/src/async_wrap.cc index 11ce5a387a950c..87c50950faadcc 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -398,6 +398,7 @@ static void DisablePromiseHook(const FunctionCallbackInfo& args) { class DestroyParam { public: double asyncId; + Environment* env; Persistent target; Persistent propBag; }; @@ -406,13 +407,12 @@ class DestroyParam { void AsyncWrap::WeakCallback(const v8::WeakCallbackInfo& info) { HandleScope scope(info.GetIsolate()); - Environment* env = Environment::GetCurrent(info.GetIsolate()); std::unique_ptr p{info.GetParameter()}; Local prop_bag = PersistentToLocal(info.GetIsolate(), p->propBag); - Local val = prop_bag->Get(env->destroyed_string()); + Local 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& args) { Isolate* isolate = args.GetIsolate(); DestroyParam* p = new DestroyParam(); p->asyncId = args[1].As()->Value(); + p->env = Environment::GetCurrent(args); p->target.Reset(isolate, args[0].As()); p->propBag.Reset(isolate, args[2].As()); p->target.SetWeak(