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 juanarbol committed Oct 11, 2022
1 parent 2f87ba4 commit 4ee8ac3
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/util.h
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 4ee8ac3

Please sign in to comment.