Skip to content

Commit

Permalink
src: do not crash when accessing empty WeakRefs
Browse files Browse the repository at this point in the history
Making `.incRef()` and `.decRef()` fail silently leads to better error
messages when trying to access the underlying value (as opposed to
crashing inside these methods).

Refs: #25461 (comment)

PR-URL: #29289
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
  • Loading branch information
addaleax authored and BridgeAR committed Sep 4, 2019
1 parent b964bdd commit f8f3af0
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,16 @@ class WeakReference : public BaseObject {

static void IncRef(const FunctionCallbackInfo<Value>& args) {
WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
if (weak_ref->reference_count_ == 0) weak_ref->target_.ClearWeak();
weak_ref->reference_count_++;
if (weak_ref->target_.IsEmpty()) return;
if (weak_ref->reference_count_ == 1) weak_ref->target_.ClearWeak();
}

static void DecRef(const FunctionCallbackInfo<Value>& args) {
WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
CHECK_GE(weak_ref->reference_count_, 1);
weak_ref->reference_count_--;
if (weak_ref->target_.IsEmpty()) return;
if (weak_ref->reference_count_ == 0) weak_ref->target_.SetWeak();
}

Expand Down

0 comments on commit f8f3af0

Please sign in to comment.