Skip to content

Commit

Permalink
src: prevent copying ArrayBufferViewContents
Browse files Browse the repository at this point in the history
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: nodejs/node#44079 (comment)
PR-URL: nodejs/node#44091
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Feng Yu <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
  • Loading branch information
kvakil authored and guangwong committed Jan 3, 2023
1 parent 245ce7a commit 3ab13b1
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,9 @@ class ArrayBufferViewContents {
public:
ArrayBufferViewContents() = default;

ArrayBufferViewContents(const ArrayBufferViewContents&) = delete;
void operator=(const ArrayBufferViewContents&) = delete;

explicit inline ArrayBufferViewContents(v8::Local<v8::Value> value);
explicit inline ArrayBufferViewContents(v8::Local<v8::Object> value);
explicit inline ArrayBufferViewContents(v8::Local<v8::ArrayBufferView> abv);
Expand All @@ -505,6 +508,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;
Expand Down

0 comments on commit 3ab13b1

Please sign in to comment.