diff --git a/AK/ByteBuffer.h b/AK/ByteBuffer.h index 86274d5117..513c505ce1 100644 --- a/AK/ByteBuffer.h +++ b/AK/ByteBuffer.h @@ -134,13 +134,12 @@ public: [[nodiscard]] void* end_pointer() { return data() + m_size; } [[nodiscard]] void const* end_pointer() const { return data() + m_size; } - // FIXME: Make this function handle failures too. - [[nodiscard]] ByteBuffer slice(size_t offset, size_t size) const + [[nodiscard]] ErrorOr slice(size_t offset, size_t size) const { // I cannot hand you a slice I don't have VERIFY(offset + size <= this->size()); - return copy(offset_pointer(offset), size).release_value(); + return copy(offset_pointer(offset), size); } void clear() diff --git a/Tests/LibC/TestSnprintf.cpp b/Tests/LibC/TestSnprintf.cpp index 1fba55947d..482d85d2af 100644 --- a/Tests/LibC/TestSnprintf.cpp +++ b/Tests/LibC/TestSnprintf.cpp @@ -72,9 +72,9 @@ static bool test_single(Testcase const& testcase) // Checking the results: bool return_ok = actual_return == testcase.expected_return; - bool canary_1_ok = actual.slice(0, SANDBOX_CANARY_SIZE) == expected.slice(0, SANDBOX_CANARY_SIZE); - bool main_ok = actual.slice(SANDBOX_CANARY_SIZE, testcase.dest_n) == expected.slice(SANDBOX_CANARY_SIZE, testcase.dest_n); - bool canary_2_ok = actual.slice(SANDBOX_CANARY_SIZE + testcase.dest_n, SANDBOX_CANARY_SIZE) == expected.slice(SANDBOX_CANARY_SIZE + testcase.dest_n, SANDBOX_CANARY_SIZE); + bool canary_1_ok = MUST(actual.slice(0, SANDBOX_CANARY_SIZE)) == MUST(expected.slice(0, SANDBOX_CANARY_SIZE)); + bool main_ok = MUST(actual.slice(SANDBOX_CANARY_SIZE, testcase.dest_n)) == MUST(expected.slice(SANDBOX_CANARY_SIZE, testcase.dest_n)); + bool canary_2_ok = MUST(actual.slice(SANDBOX_CANARY_SIZE + testcase.dest_n, SANDBOX_CANARY_SIZE)) == MUST(expected.slice(SANDBOX_CANARY_SIZE + testcase.dest_n, SANDBOX_CANARY_SIZE)); bool buf_ok = actual == expected; // Evaluate gravity: @@ -85,20 +85,20 @@ static bool test_single(Testcase const& testcase) if (!canary_1_ok) { warnln("Canary 1 overwritten: Expected {}\n" " instead got {}", - show(expected.slice(0, SANDBOX_CANARY_SIZE)), - show(actual.slice(0, SANDBOX_CANARY_SIZE))); + show(MUST(expected.slice(0, SANDBOX_CANARY_SIZE))), + show(MUST(actual.slice(0, SANDBOX_CANARY_SIZE)))); } if (!main_ok) { warnln("Wrong output: Expected {}\n" " instead, got {}", - show(expected.slice(SANDBOX_CANARY_SIZE, testcase.dest_n)), - show(actual.slice(SANDBOX_CANARY_SIZE, testcase.dest_n))); + show(MUST(expected.slice(SANDBOX_CANARY_SIZE, testcase.dest_n))), + show(MUST(actual.slice(SANDBOX_CANARY_SIZE, testcase.dest_n)))); } if (!canary_2_ok) { warnln("Canary 2 overwritten: Expected {}\n" " instead, got {}", - show(expected.slice(SANDBOX_CANARY_SIZE + testcase.dest_n, SANDBOX_CANARY_SIZE)), - show(actual.slice(SANDBOX_CANARY_SIZE + testcase.dest_n, SANDBOX_CANARY_SIZE))); + show(MUST(expected.slice(SANDBOX_CANARY_SIZE + testcase.dest_n, SANDBOX_CANARY_SIZE))), + show(MUST(actual.slice(SANDBOX_CANARY_SIZE + testcase.dest_n, SANDBOX_CANARY_SIZE)))); } if (!return_ok) { warnln("Wrong return value: Expected {}, got {} instead!", testcase.expected_return, actual_return); diff --git a/Tests/LibC/TestStrlcpy.cpp b/Tests/LibC/TestStrlcpy.cpp index 407c307e2f..58106e51a1 100644 --- a/Tests/LibC/TestStrlcpy.cpp +++ b/Tests/LibC/TestStrlcpy.cpp @@ -69,9 +69,9 @@ static bool test_single(Testcase const& testcase) // Checking the results: bool return_ok = actual_return == testcase.src_n; - bool canary_1_ok = actual.slice(0, SANDBOX_CANARY_SIZE) == expected.slice(0, SANDBOX_CANARY_SIZE); - bool main_ok = actual.slice(SANDBOX_CANARY_SIZE, testcase.dest_n) == expected.slice(SANDBOX_CANARY_SIZE, testcase.dest_n); - bool canary_2_ok = actual.slice(SANDBOX_CANARY_SIZE + testcase.dest_n, SANDBOX_CANARY_SIZE) == expected.slice(SANDBOX_CANARY_SIZE + testcase.dest_n, SANDBOX_CANARY_SIZE); + bool canary_1_ok = MUST(actual.slice(0, SANDBOX_CANARY_SIZE)) == MUST(expected.slice(0, SANDBOX_CANARY_SIZE)); + bool main_ok = MUST(actual.slice(SANDBOX_CANARY_SIZE, testcase.dest_n)) == MUST(expected.slice(SANDBOX_CANARY_SIZE, testcase.dest_n)); + bool canary_2_ok = MUST(actual.slice(SANDBOX_CANARY_SIZE + testcase.dest_n, SANDBOX_CANARY_SIZE)) == MUST(expected.slice(SANDBOX_CANARY_SIZE + testcase.dest_n, SANDBOX_CANARY_SIZE)); bool buf_ok = actual == expected; // Evaluate gravity: @@ -82,20 +82,20 @@ static bool test_single(Testcase const& testcase) if (!canary_1_ok) { warnln("Canary 1 overwritten: Expected canary {}\n" " instead got {}", - show(expected.slice(0, SANDBOX_CANARY_SIZE)), - show(actual.slice(0, SANDBOX_CANARY_SIZE))); + show(MUST(expected.slice(0, SANDBOX_CANARY_SIZE))), + show(MUST(actual.slice(0, SANDBOX_CANARY_SIZE)))); } if (!main_ok) { warnln("Wrong output: Expected {}\n" " instead got {}", - show(expected.slice(SANDBOX_CANARY_SIZE, testcase.dest_n)), - show(actual.slice(SANDBOX_CANARY_SIZE, testcase.dest_n))); + show(MUST(expected.slice(SANDBOX_CANARY_SIZE, testcase.dest_n))), + show(MUST(actual.slice(SANDBOX_CANARY_SIZE, testcase.dest_n)))); } if (!canary_2_ok) { warnln("Canary 2 overwritten: Expected {}\n" " instead got {}", - show(expected.slice(SANDBOX_CANARY_SIZE + testcase.dest_n, SANDBOX_CANARY_SIZE)), - show(actual.slice(SANDBOX_CANARY_SIZE + testcase.dest_n, SANDBOX_CANARY_SIZE))); + show(MUST(expected.slice(SANDBOX_CANARY_SIZE + testcase.dest_n, SANDBOX_CANARY_SIZE))), + show(MUST(actual.slice(SANDBOX_CANARY_SIZE + testcase.dest_n, SANDBOX_CANARY_SIZE)))); } if (!return_ok) { warnln("Wrong return value: Expected {}, got {} instead!", testcase.src_n, actual_return); diff --git a/Userland/Libraries/LibGemini/Job.cpp b/Userland/Libraries/LibGemini/Job.cpp index f7cd212d0f..6196abf318 100644 --- a/Userland/Libraries/LibGemini/Job.cpp +++ b/Userland/Libraries/LibGemini/Job.cpp @@ -64,7 +64,8 @@ ByteBuffer Job::receive(size_t size) { ByteBuffer buffer = ByteBuffer::create_uninitialized(size).release_value_but_fixme_should_propagate_errors(); auto nread = MUST(m_socket->read(buffer)).size(); - return buffer.slice(0, nread); + // FIXME: Propagate errors. + return MUST(buffer.slice(0, nread)); } bool Job::can_read() const @@ -101,7 +102,8 @@ void Job::flush_received_buffers() continue; } VERIFY(written < payload.size()); - payload = payload.slice(written, payload.size() - written); + // FIXME: Propagate errors. + payload = MUST(payload.slice(written, payload.size() - written)); return; } } diff --git a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h index 6dd93aecb7..8cb6aae419 100644 --- a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h +++ b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h @@ -130,7 +130,8 @@ Value ArrayBuffer::get_value(size_t byte_index, [[maybe_unused]] bool is_typed_a // FIXME: Check for shared buffer - auto raw_value = buffer_impl().slice(byte_index, element_size); + // FIXME: Propagate errors. + auto raw_value = MUST(buffer_impl().slice(byte_index, element_size)); return raw_bytes_to_numeric(global_object(), move(raw_value), is_little_endian); } @@ -218,7 +219,8 @@ Value ArrayBuffer::get_modify_set_value(size_t byte_index, Value value, ReadWrit // FIXME: Check for shared buffer - auto raw_bytes_read = buffer_impl().slice(byte_index, sizeof(T)); + // FIXME: Propagate errors. + auto raw_bytes_read = MUST(buffer_impl().slice(byte_index, sizeof(T))); auto raw_bytes_modified = operation(raw_bytes_read, raw_bytes); raw_bytes_modified.span().copy_to(buffer_impl().span().slice(byte_index)); diff --git a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp index 5838ae9bb4..c677a21431 100644 --- a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp @@ -246,7 +246,8 @@ static ThrowCompletionOr atomic_compare_exchange_impl(GlobalObject& globa // 14. Else, // a. Let rawBytesRead be a List of length elementSize whose elements are the sequence of elementSize bytes starting with block[indexedPosition]. - auto raw_bytes_read = block.slice(indexed_position, sizeof(T)); + // FIXME: Propagate errors. + auto raw_bytes_read = MUST(block.slice(indexed_position, sizeof(T))); // b. If ByteListEqual(rawBytesRead, expectedBytes) is true, then // i. Store the individual bytes of replacementBytes into block, starting at block[indexedPosition]. diff --git a/Userland/Libraries/LibSQL/Heap.cpp b/Userland/Libraries/LibSQL/Heap.cpp index 813909df3e..396172b67f 100644 --- a/Userland/Libraries/LibSQL/Heap.cpp +++ b/Userland/Libraries/LibSQL/Heap.cpp @@ -202,7 +202,7 @@ constexpr static int USER_VALUES_OFFSET = 32; ErrorOr Heap::read_zero_block() { auto buffer = TRY(read_block(0)); - auto file_id_buffer = buffer.slice(0, FILE_ID.length()); + auto file_id_buffer = TRY(buffer.slice(0, FILE_ID.length())); auto file_id = StringView(file_id_buffer); if (file_id != FILE_ID) { warnln("{}: Zero page corrupt. This is probably not a {} heap file"sv, name(), FILE_ID); diff --git a/Userland/Libraries/LibTLS/Socket.cpp b/Userland/Libraries/LibTLS/Socket.cpp index a13e2a4999..fa75c85c54 100644 --- a/Userland/Libraries/LibTLS/Socket.cpp +++ b/Userland/Libraries/LibTLS/Socket.cpp @@ -27,8 +27,8 @@ ErrorOr TLSv12::read(Bytes bytes) return Bytes {}; } - m_context.application_buffer.span().slice(0, size_to_read).copy_to(bytes); - m_context.application_buffer = m_context.application_buffer.slice(size_to_read, m_context.application_buffer.size() - size_to_read); + TRY(m_context.application_buffer.slice(0, size_to_read)).span().copy_to(bytes); + m_context.application_buffer = TRY(m_context.application_buffer.slice(size_to_read, m_context.application_buffer.size() - size_to_read)); return Bytes { bytes.data(), size_to_read }; } @@ -47,7 +47,8 @@ String TLSv12::read_line(size_t max_size) return {}; String line { bit_cast(start), offset, Chomp }; - m_context.application_buffer = m_context.application_buffer.slice(offset + 1, m_context.application_buffer.size() - offset - 1); + // FIXME: Propagate errors. + m_context.application_buffer = MUST(m_context.application_buffer.slice(offset + 1, m_context.application_buffer.size() - offset - 1)); return line; } diff --git a/Userland/Libraries/LibTLS/TLSPacketBuilder.h b/Userland/Libraries/LibTLS/TLSPacketBuilder.h index 6c864a3700..407e30d0f1 100644 --- a/Userland/Libraries/LibTLS/TLSPacketBuilder.h +++ b/Userland/Libraries/LibTLS/TLSPacketBuilder.h @@ -85,7 +85,8 @@ public: { auto length = m_current_length; m_current_length = 0; - return m_packet_data.slice(0, length); + // FIXME: Propagate errors. + return MUST(m_packet_data.slice(0, length)); } inline void set(size_t offset, u8 value) { diff --git a/Userland/Libraries/LibTLS/TLSv12.cpp b/Userland/Libraries/LibTLS/TLSv12.cpp index 9311384002..2f896b4045 100644 --- a/Userland/Libraries/LibTLS/TLSv12.cpp +++ b/Userland/Libraries/LibTLS/TLSv12.cpp @@ -92,7 +92,8 @@ void TLSv12::consume(ReadonlyBytes record) } if (index) { - m_context.message_buffer = m_context.message_buffer.slice(index, m_context.message_buffer.size() - index); + // FIXME: Propagate errors. + m_context.message_buffer = MUST(m_context.message_buffer.slice(index, m_context.message_buffer.size() - index)); } } diff --git a/Userland/Utilities/lspci.cpp b/Userland/Utilities/lspci.cpp index 2b6dd91fe4..53f6b8c958 100644 --- a/Userland/Utilities/lspci.cpp +++ b/Userland/Utilities/lspci.cpp @@ -27,7 +27,10 @@ static constexpr StringView format_region = "\tBAR {}: {} region @ {:#x}"; static u32 read_hex_string_from_bytebuffer(ByteBuffer const& buf) { - return AK::StringUtils::convert_to_uint_from_hex(String(buf.slice(2, buf.size() - 2).bytes())).release_value(); + // FIXME: Propagate errors. + return AK::StringUtils::convert_to_uint_from_hex( + String(MUST(buf.slice(2, buf.size() - 2)).bytes())) + .release_value(); } static u32 convert_sysfs_value_to_uint(String const& value) diff --git a/Userland/Utilities/pro.cpp b/Userland/Utilities/pro.cpp index 93201b0f26..775eafa585 100644 --- a/Userland/Utilities/pro.cpp +++ b/Userland/Utilities/pro.cpp @@ -132,7 +132,8 @@ private: if (!m_buffer.is_empty()) { auto size = OutputFileStream::write(m_buffer); - m_buffer = m_buffer.slice(size, m_buffer.size() - size); + // FIXME: Propagate errors. + m_buffer = MUST(m_buffer.slice(size, m_buffer.size() - size)); } if (!m_buffer.is_empty())