From 120ac7435255a158117feb03be654717a3fb431c Mon Sep 17 00:00:00 2001 From: legendecas Date: Tue, 9 May 2023 01:29:16 +0800 Subject: [PATCH] src: remove aliased buffer weak callback An AliasedBuffer can be nested in a BaseObject and their weak callbacks invoke order are not guaranteed. Prevent aliased buffer from being accessed in its weak callback as its validness can be checked with the emptiness of the persistent handle. PR-URL: https://github.com/nodejs/node/pull/47688 Reviewed-By: Matteo Collina Reviewed-By: Joyee Cheung --- src/aliased_buffer-inl.h | 15 +++------------ src/aliased_buffer.h | 3 --- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/src/aliased_buffer-inl.h b/src/aliased_buffer-inl.h index 782a125570d06d..f04fb7c2667016 100644 --- a/src/aliased_buffer-inl.h +++ b/src/aliased_buffer-inl.h @@ -70,8 +70,8 @@ AliasedBufferBase::AliasedBufferBase( count_(that.count_), byte_offset_(that.byte_offset_), buffer_(that.buffer_) { - DCHECK(is_valid()); js_array_ = v8::Global(that.isolate_, that.GetJSArray()); + DCHECK(is_valid()); } template @@ -126,19 +126,10 @@ void AliasedBufferBase::Release() { js_array_.Reset(); } -template -inline void AliasedBufferBase::WeakCallback( - const v8::WeakCallbackInfo>& data) { - AliasedBufferBase* buffer = data.GetParameter(); - DCHECK(buffer->is_valid()); - buffer->cleared_ = true; - buffer->js_array_.Reset(); -} - template inline void AliasedBufferBase::MakeWeak() { DCHECK(is_valid()); - js_array_.SetWeak(this, WeakCallback, v8::WeakCallbackType::kParameter); + js_array_.SetWeak(); } template @@ -223,7 +214,7 @@ void AliasedBufferBase::reserve(size_t new_capacity) { template inline bool AliasedBufferBase::is_valid() const { - return index_ == nullptr && !cleared_; + return index_ == nullptr && !js_array_.IsEmpty(); } template diff --git a/src/aliased_buffer.h b/src/aliased_buffer.h index 4c4f8ac21dfc4a..b847641f8faa15 100644 --- a/src/aliased_buffer.h +++ b/src/aliased_buffer.h @@ -173,14 +173,11 @@ class AliasedBufferBase : public MemoryRetainer { private: inline bool is_valid() const; - static inline void WeakCallback( - const v8::WeakCallbackInfo>& data); v8::Isolate* isolate_ = nullptr; size_t count_ = 0; size_t byte_offset_ = 0; NativeT* buffer_ = nullptr; v8::Global js_array_; - bool cleared_ = false; // Deserialize data const AliasedBufferIndex* index_ = nullptr;