From df8f074cf614aeffc127c6f60317b7a6f80bbf63 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 21 Feb 2021 11:40:35 +0100 Subject: [PATCH] LibJS: Make TypedArray::data() return a Span This inserts bounds checking assertions whenever we're reading/writing a typed array from JS. --- Userland/Libraries/LibJS/Runtime/TypedArray.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.h b/Userland/Libraries/LibJS/Runtime/TypedArray.h index 4e29ef6fd6..a0f6709a76 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.h +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.h @@ -50,7 +50,7 @@ public: virtual size_t element_size() const = 0; protected: - TypedArrayBase(Object& prototype) + explicit TypedArrayBase(Object& prototype) : Object(prototype) { } @@ -116,7 +116,14 @@ public: } } - T* data() const { return reinterpret_cast(m_viewed_array_buffer->buffer().data()); } + Span data() const + { + return { reinterpret_cast(m_viewed_array_buffer->buffer().data()), m_array_length }; + } + Span data() + { + return { reinterpret_cast(m_viewed_array_buffer->buffer().data()), m_array_length }; + } virtual size_t element_size() const override { return sizeof(T); }; @@ -127,7 +134,7 @@ protected: ASSERT(!Checked::multiplication_would_overflow(array_length, sizeof(T))); m_viewed_array_buffer = ArrayBuffer::create(global_object(), array_length * sizeof(T)); if (array_length) - ASSERT(data() != nullptr); + ASSERT(!data().is_null()); m_array_length = array_length; m_byte_length = m_viewed_array_buffer->byte_length(); }