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)
  • Loading branch information
kvakil committed Aug 2, 2022
1 parent 07d7e1b commit d1f3368
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 d1f3368

Please sign in to comment.