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: #44079 (comment)
PR-URL: #44091
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Feng Yu <F3n67u@outlook.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
  • Loading branch information
kvakil authored and danielleadams committed Aug 15, 2022
1 parent 6256bd5 commit 8082c24
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 @@ -498,6 +498,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 @@ -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;
Expand Down

0 comments on commit 8082c24

Please sign in to comment.