From 28042057b64e6693ccebd81d1840bb463f57c839 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 24 Aug 2019 02:12:42 +0200 Subject: [PATCH] src: do not crash when accessing empty WeakRefs 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: https://github.com/nodejs/node/pull/25461#issuecomment-524481482 --- src/node_util.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/node_util.cc b/src/node_util.cc index fa39583b04ba3c..9c24985a47a507 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -191,14 +191,16 @@ class WeakReference : public BaseObject { static void IncRef(const FunctionCallbackInfo& args) { WeakReference* weak_ref = Unwrap(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& args) { WeakReference* weak_ref = Unwrap(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(); }