summaryrefslogtreecommitdiff
path: root/src/node_util.cc
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2019-08-24 02:12:42 +0200
committerRich Trott <rtrott@gmail.com>2019-09-01 23:10:54 -0700
commit2cc757f0c703598c1bc5777e79c9344c946df2cc (patch)
treed18cf92ba5bda8b0464e05eff4ceacf09a5c1cf0 /src/node_util.cc
parent0f3aa81eab383ab85b5b84d6fe4570115d824a64 (diff)
downloadandroid-node-v8-2cc757f0c703598c1bc5777e79c9344c946df2cc.tar.gz
android-node-v8-2cc757f0c703598c1bc5777e79c9344c946df2cc.tar.bz2
android-node-v8-2cc757f0c703598c1bc5777e79c9344c946df2cc.zip
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 PR-URL: https://github.com/nodejs/node/pull/29289 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Gus Caplan <me@gus.host>
Diffstat (limited to 'src/node_util.cc')
-rw-r--r--src/node_util.cc4
1 files changed, 3 insertions, 1 deletions
diff --git a/src/node_util.cc b/src/node_util.cc
index fa39583b04..9c24985a47 100644
--- a/src/node_util.cc
+++ b/src/node_util.cc
@@ -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();
}