Skip to content

Commit

Permalink
src: store pointer to Environment on DestroyParam
Browse files Browse the repository at this point in the history
To avoid a potential segfault when inside WeakCallback, store a
reference to Environment inside DestroyParam.

PR-URL: #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>
  • Loading branch information
3 people authored and MylesBorins committed Jun 6, 2018
1 parent 9d41ab4 commit 8862f0a
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ static void DisablePromiseHook(const FunctionCallbackInfo<Value>& args) {
class DestroyParam {
public:
double asyncId;
Environment* env;
Persistent<Object> target;
Persistent<Object> propBag;
};
Expand All @@ -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.
}
Expand All @@ -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(
Expand Down

0 comments on commit 8862f0a

Please sign in to comment.