Skip to content

Commit 0300f7c

Browse files
apapirovskihashseedtargos
committedJun 5, 2018
src: store pointer to Environment on DestroyParam
To avoid a potential segfault when inside WeakCallback, store a reference to Environment inside DestroyParam. PR-URL: #21099 Reviewed-By: Yang Guo <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Co-authored-by: Yang Guo <[email protected]> Co-authored-by: Michaël Zasso <[email protected]>
1 parent 3c5b8b4 commit 0300f7c

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed
 

‎src/async_wrap.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ static void DisablePromiseHook(const FunctionCallbackInfo<Value>& args) {
398398
class DestroyParam {
399399
public:
400400
double asyncId;
401+
Environment* env;
401402
Persistent<Object> target;
402403
Persistent<Object> propBag;
403404
};
@@ -406,13 +407,12 @@ class DestroyParam {
406407
void AsyncWrap::WeakCallback(const v8::WeakCallbackInfo<DestroyParam>& info) {
407408
HandleScope scope(info.GetIsolate());
408409

409-
Environment* env = Environment::GetCurrent(info.GetIsolate());
410410
std::unique_ptr<DestroyParam> p{info.GetParameter()};
411411
Local<Object> prop_bag = PersistentToLocal(info.GetIsolate(), p->propBag);
412412

413-
Local<Value> val = prop_bag->Get(env->destroyed_string());
413+
Local<Value> val = prop_bag->Get(p->env->destroyed_string());
414414
if (val->IsFalse()) {
415-
AsyncWrap::EmitDestroy(env, p->asyncId);
415+
AsyncWrap::EmitDestroy(p->env, p->asyncId);
416416
}
417417
// unique_ptr goes out of scope here and pointer is deleted.
418418
}
@@ -426,6 +426,7 @@ static void RegisterDestroyHook(const FunctionCallbackInfo<Value>& args) {
426426
Isolate* isolate = args.GetIsolate();
427427
DestroyParam* p = new DestroyParam();
428428
p->asyncId = args[1].As<Number>()->Value();
429+
p->env = Environment::GetCurrent(args);
429430
p->target.Reset(isolate, args[0].As<Object>());
430431
p->propBag.Reset(isolate, args[2].As<Object>());
431432
p->target.SetWeak(

0 commit comments

Comments
 (0)
Please sign in to comment.