Skip to content

Commit 3bb15d3

Browse files
shannonboothlinusg
authored andcommitted
LibJS: Propagate OOM from GetValueFromBuffer AO
1 parent 49c8d22 commit 3bb15d3

File tree

5 files changed

+12
-13
lines changed

5 files changed

+12
-13
lines changed

Userland/Libraries/LibJS/Runtime/ArrayBuffer.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class ArrayBuffer : public Object {
7272
Unordered
7373
};
7474
template<typename type>
75-
Value get_value(size_t byte_index, bool is_typed_array, Order, bool is_little_endian = true);
75+
ThrowCompletionOr<Value> get_value(size_t byte_index, bool is_typed_array, Order, bool is_little_endian = true);
7676
template<typename type>
7777
void set_value(size_t byte_index, Value value, bool is_typed_array, Order, bool is_little_endian = true);
7878
template<typename T>
@@ -149,16 +149,15 @@ static Value raw_bytes_to_numeric(VM& vm, ByteBuffer raw_value, bool is_little_e
149149

150150
// Implementation for 25.1.2.10 GetValueFromBuffer, used in TypedArray<T>::get_value_from_buffer().
151151
template<typename T>
152-
Value ArrayBuffer::get_value(size_t byte_index, [[maybe_unused]] bool is_typed_array, Order, bool is_little_endian)
152+
ThrowCompletionOr<Value> ArrayBuffer::get_value(size_t byte_index, [[maybe_unused]] bool is_typed_array, Order, bool is_little_endian)
153153
{
154154
auto& vm = this->vm();
155155

156156
auto element_size = sizeof(T);
157157

158158
// FIXME: Check for shared buffer
159159

160-
// FIXME: Propagate errors.
161-
auto raw_value = MUST(buffer_impl().slice(byte_index, element_size));
160+
auto raw_value = TRY_OR_THROW_OOM(vm, buffer_impl().slice(byte_index, element_size));
162161
return raw_bytes_to_numeric<T>(vm, move(raw_value), is_little_endian);
163162
}
164163

Userland/Libraries/LibJS/Runtime/TypedArray.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ static ThrowCompletionOr<void> initialize_typed_array_from_typed_array(VM& vm, T
189189
// g. Repeat, while count > 0,
190190
for (u32 i = 0; i < element_length; ++i) {
191191
// i. Let value be GetValueFromBuffer(srcData, srcByteIndex, srcType, true, Unordered).
192-
auto value = src_array.get_value_from_buffer(src_byte_index, ArrayBuffer::Order::Unordered);
192+
auto value = MUST_OR_THROW_OOM(src_array.get_value_from_buffer(src_byte_index, ArrayBuffer::Order::Unordered));
193193

194194
// ii. Perform SetValueInBuffer(data, targetByteIndex, elementType, value, true, Unordered).
195195
data->template set_value<T>(target_byte_index, value, true, ArrayBuffer::Order::Unordered);

Userland/Libraries/LibJS/Runtime/TypedArray.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class TypedArrayBase : public Object {
5454
// 25.1.2.7 IsBigIntElementType ( type ), https://tc39.es/ecma262/#sec-isbigintelementtype
5555
virtual bool is_bigint_element_type() const = 0;
5656
// 25.1.2.10 GetValueFromBuffer ( arrayBuffer, byteIndex, type, isTypedArray, order [ , isLittleEndian ] ), https://tc39.es/ecma262/#sec-getvaluefrombuffer
57-
virtual Value get_value_from_buffer(size_t byte_index, ArrayBuffer::Order, bool is_little_endian = true) const = 0;
57+
virtual ThrowCompletionOr<Value> get_value_from_buffer(size_t byte_index, ArrayBuffer::Order, bool is_little_endian = true) const = 0;
5858
// 25.1.2.12 SetValueInBuffer ( arrayBuffer, byteIndex, type, value, isTypedArray, order [ , isLittleEndian ] ), https://tc39.es/ecma262/#sec-setvalueinbuffer
5959
virtual void set_value_in_buffer(size_t byte_index, Value, ArrayBuffer::Order, bool is_little_endian = true) = 0;
6060
// 25.1.2.13 GetModifySetValueInBuffer ( arrayBuffer, byteIndex, type, value, op [ , isLittleEndian ] ), https://tc39.es/ecma262/#sec-getmodifysetvalueinbuffer
@@ -100,7 +100,7 @@ inline bool is_valid_integer_index(TypedArrayBase const& typed_array, CanonicalI
100100

101101
// 10.4.5.10 IntegerIndexedElementGet ( O, index ), https://tc39.es/ecma262/#sec-integerindexedelementget
102102
template<typename T>
103-
inline Value integer_indexed_element_get(TypedArrayBase const& typed_array, CanonicalIndex property_index)
103+
inline ThrowCompletionOr<Value> integer_indexed_element_get(TypedArrayBase const& typed_array, CanonicalIndex property_index)
104104
{
105105
// 1. If IsValidIntegerIndex(O, index) is false, return undefined.
106106
if (!is_valid_integer_index(typed_array, property_index))
@@ -195,7 +195,7 @@ class TypedArray : public TypedArrayBase {
195195
// b. If numericIndex is not undefined, then
196196
if (!numeric_index.is_undefined()) {
197197
// i. Let value be IntegerIndexedElementGet(O, numericIndex).
198-
auto value = integer_indexed_element_get<T>(*this, numeric_index);
198+
auto value = MUST_OR_THROW_OOM(integer_indexed_element_get<T>(*this, numeric_index));
199199

200200
// ii. If value is undefined, return undefined.
201201
if (value.is_undefined())
@@ -437,7 +437,7 @@ class TypedArray : public TypedArrayBase {
437437
return is_bigint;
438438
}
439439

440-
Value get_value_from_buffer(size_t byte_index, ArrayBuffer::Order order, bool is_little_endian = true) const override { return viewed_array_buffer()->template get_value<T>(byte_index, true, order, is_little_endian); }
440+
ThrowCompletionOr<Value> get_value_from_buffer(size_t byte_index, ArrayBuffer::Order order, bool is_little_endian = true) const override { return viewed_array_buffer()->template get_value<T>(byte_index, true, order, is_little_endian); }
441441
void set_value_in_buffer(size_t byte_index, Value value, ArrayBuffer::Order order, bool is_little_endian = true) override { viewed_array_buffer()->template set_value<T>(byte_index, value, true, order, is_little_endian); }
442442
ThrowCompletionOr<Value> get_modify_set_value_in_buffer(size_t byte_index, Value value, ReadWriteModifyFunction operation, bool is_little_endian = true) override { return viewed_array_buffer()->template get_modify_set_value<T>(byte_index, value, move(operation), is_little_endian); }
443443

Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::copy_within)
415415
// k. Repeat, while countBytes > 0,
416416
for (; count_bytes > 0; --count_bytes) {
417417
// i. Let value be GetValueFromBuffer(buffer, fromByteIndex, Uint8, true, Unordered).
418-
auto value = buffer->get_value<u8>(from_byte_index, true, ArrayBuffer::Order::Unordered);
418+
auto value = MUST_OR_THROW_OOM(buffer->get_value<u8>(from_byte_index, true, ArrayBuffer::Order::Unordered));
419419

420420
// ii. Perform SetValueInBuffer(buffer, toByteIndex, Uint8, value, true, Unordered).
421421
buffer->set_value<u8>(to_byte_index, value, true, ArrayBuffer::Order::Unordered);
@@ -1231,7 +1231,7 @@ static ThrowCompletionOr<void> set_typed_array_from_typed_array(VM& vm, TypedArr
12311231
// a. Repeat, while targetByteIndex < limit,
12321232
while (target_byte_index < limit) {
12331233
// i. Let value be GetValueFromBuffer(srcBuffer, srcByteIndex, srcType, true, Unordered).
1234-
auto value = source.get_value_from_buffer(source_byte_index, ArrayBuffer::Unordered);
1234+
auto value = MUST_OR_THROW_OOM(source.get_value_from_buffer(source_byte_index, ArrayBuffer::Unordered));
12351235
// ii. Perform SetValueInBuffer(targetBuffer, targetByteIndex, targetType, value, true, Unordered).
12361236
target.set_value_in_buffer(target_byte_index, value, ArrayBuffer::Unordered);
12371237
// iii. Set srcByteIndex to srcByteIndex + srcElementSize.
@@ -1459,7 +1459,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::slice)
14591459
// ix. Repeat, while targetByteIndex < limit,
14601460
for (; target_byte_index < limit.value(); ++source_byte_index, ++target_byte_index) {
14611461
// 1. Let value be GetValueFromBuffer(srcBuffer, srcByteIndex, Uint8, true, Unordered).
1462-
auto value = source_buffer.get_value<u8>(source_byte_index.value(), true, ArrayBuffer::Unordered);
1462+
auto value = MUST_OR_THROW_OOM(source_buffer.get_value<u8>(source_byte_index.value(), true, ArrayBuffer::Unordered));
14631463

14641464
// 2. Perform SetValueInBuffer(targetBuffer, targetByteIndex, Uint8, value, true, Unordered).
14651465
target_buffer.set_value<u8>(target_byte_index, value, true, ArrayBuffer::Unordered);

Userland/Libraries/LibWeb/WebIDL/AbstractOperations.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ ErrorOr<ByteBuffer> get_buffer_source_copy(JS::Object const& buffer_source)
7474

7575
// 9. For i in the range offset to offset + length − 1, inclusive, set bytes[i − offset] to ! GetValueFromBuffer(esArrayBuffer, i, Uint8, true, Unordered).
7676
for (u64 i = offset; i < offset + length; ++i) {
77-
auto value = es_array_buffer->get_value<u8>(i, true, JS::ArrayBuffer::Unordered);
77+
auto value = es_array_buffer->get_value<u8>(i, true, JS::ArrayBuffer::Unordered).release_allocated_value_but_fixme_should_propagate_errors();
7878
bytes[i - offset] = static_cast<u8>(value.as_double());
7979
}
8080

0 commit comments

Comments
 (0)