From d1f3368acbddd77305390c5eeeb8d31e4afa3390 Mon Sep 17 00:00:00 2001 From: Keyhan Vakil Date: Tue, 2 Aug 2022 00:32:25 +0000 Subject: [PATCH 1/2] src: prevent copying ArrayBufferViewContents It is error-prone to copy or heap-allocate `ArrayBufferViewContents`, because you might accidentally cause it to exceed the lifetime of its argument. Let's make it impossible to do so. Fortunately we were not doing so anywhere already, so this diff is purely defensive. Refs: https://github.com/nodejs/node/pull/44079#discussion_r934376046 --- src/util.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/util.h b/src/util.h index a48071b093db97..b29781d27749c3 100644 --- a/src/util.h +++ b/src/util.h @@ -498,6 +498,9 @@ class ArrayBufferViewContents { public: ArrayBufferViewContents() = default; + ArrayBufferViewContents(const ArrayBufferViewContents&) = delete; + void operator=(const ArrayBufferViewContents&) = delete; + explicit inline ArrayBufferViewContents(v8::Local value); explicit inline ArrayBufferViewContents(v8::Local value); explicit inline ArrayBufferViewContents(v8::Local abv); @@ -507,6 +510,13 @@ class ArrayBufferViewContents { inline size_t length() const { return length_; } private: + // Declaring operator new and delete as deleted is not spec compliant. + // Therefore declare them private instead to disable dynamic alloc + void* operator new(size_t size); + void* operator new[](size_t size); + void operator delete(void*, size_t); + void operator delete[](void*, size_t); + T stack_storage_[kStackStorageSize]; T* data_ = nullptr; size_t length_ = 0; From d0522f5d835fdbb8f81cdbe266e79dfd263202ee Mon Sep 17 00:00:00 2001 From: Keyhan Vakil Date: Thu, 4 Aug 2022 20:58:16 -0700 Subject: [PATCH 2/2] fixup! src: prevent copying ArrayBufferViewContents --- src/util.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.h b/src/util.h index b29781d27749c3..011d6b19cd0186 100644 --- a/src/util.h +++ b/src/util.h @@ -511,7 +511,7 @@ class ArrayBufferViewContents { private: // Declaring operator new and delete as deleted is not spec compliant. - // Therefore declare them private instead to disable dynamic alloc + // Therefore, declare them private instead to disable dynamic alloc. void* operator new(size_t size); void* operator new[](size_t size); void operator delete(void*, size_t);