0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

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>
This commit is contained in:
Anna Henningsen 2019-08-24 02:12:42 +02:00 committed by Rich Trott
parent 0f3aa81eab
commit 2cc757f0c7

View File

@ -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();
}