summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2022-01-20 17:47:39 +0000
committerAndreas Kling <kling@serenityos.org>2022-01-24 22:36:09 +0100
commit45cf40653a03dab11c0739783446ff696a9a5b0a (patch)
tree1611c797d1a43a106cf7220fcbdbba907f19d037
parent140f1d9e55bfacb6f784bee591a6938714ed95b3 (diff)
downloadserenity-45cf40653a03dab11c0739783446ff696a9a5b0a.zip
Everywhere: Convert ByteBuffer factory methods from Optional -> ErrorOr
Apologies for the enormous commit, but I don't see a way to split this up nicely. In the vast majority of cases it's a simple change. A few extra places can use TRY instead of manual error checking though. :^)
-rw-r--r--AK/Base64.cpp2
-rw-r--r--AK/ByteBuffer.h26
-rw-r--r--AK/Hex.cpp4
-rw-r--r--AK/MemoryStream.h4
-rw-r--r--AK/String.cpp2
-rw-r--r--AK/StringBuilder.cpp2
-rw-r--r--Kernel/FileSystem/BlockBasedFileSystem.cpp5
-rw-r--r--Kernel/FileSystem/Ext2FileSystem.cpp35
-rw-r--r--Kernel/Graphics/Bochs/GraphicsAdapter.cpp5
-rw-r--r--Kernel/Graphics/Intel/NativeGraphicsAdapter.cpp8
-rw-r--r--Kernel/Graphics/VirtIOGPU/GraphicsAdapter.cpp15
-rw-r--r--Kernel/Net/NE2000/NetworkAdapter.cpp4
-rw-r--r--Kernel/Net/NetworkAdapter.cpp4
-rw-r--r--Kernel/Random.h6
-rw-r--r--Kernel/Storage/ATA/IDEChannel.cpp4
-rw-r--r--Kernel/Storage/NVMe/NVMeController.cpp2
-rw-r--r--Kernel/Storage/Partition/GUIDPartitionTable.cpp4
-rw-r--r--Kernel/Storage/Partition/MBRPartitionTable.cpp4
-rw-r--r--Kernel/Storage/StorageDevice.cpp7
-rw-r--r--Tests/AK/TestByteBuffer.cpp4
-rw-r--r--Tests/Kernel/stress-writeread.cpp2
-rw-r--r--Tests/LibCore/TestLibCoreStream.cpp18
-rw-r--r--Tests/LibIMAP/TestQuotedPrintable.cpp2
-rw-r--r--Userland/Applications/Browser/BrowserWindow.cpp6
-rw-r--r--Userland/Applications/Browser/main.cpp18
-rw-r--r--Userland/Applications/HexEditor/HexEditor.cpp4
-rw-r--r--Userland/Applications/Spreadsheet/ExportDialog.cpp2
-rw-r--r--Userland/DevTools/UserspaceEmulator/Emulator_syscalls.cpp30
-rw-r--r--Userland/DevTools/UserspaceEmulator/SoftMMU.cpp2
-rw-r--r--Userland/Libraries/LibAudio/FlacLoader.cpp2
-rw-r--r--Userland/Libraries/LibAudio/WavLoader.cpp2
-rw-r--r--Userland/Libraries/LibCore/IODevice.cpp8
-rw-r--r--Userland/Libraries/LibCore/InputBitStream.h10
-rw-r--r--Userland/Libraries/LibCore/SecretString.cpp5
-rw-r--r--Userland/Libraries/LibCore/Stream.h6
-rw-r--r--Userland/Libraries/LibCore/UDPServer.cpp2
-rw-r--r--Userland/Libraries/LibCoredump/Reader.cpp11
-rw-r--r--Userland/Libraries/LibCrypto/Cipher/Mode/GCM.h8
-rw-r--r--Userland/Libraries/LibCrypto/Cipher/Mode/Mode.h2
-rw-r--r--Userland/Libraries/LibCrypto/NumberTheory/ModularFunctions.cpp2
-rw-r--r--Userland/Libraries/LibCrypto/PK/RSA.cpp2
-rw-r--r--Userland/Libraries/LibEDID/EDID.cpp14
-rw-r--r--Userland/Libraries/LibELF/DynamicLinker.cpp2
-rw-r--r--Userland/Libraries/LibGUI/Clipboard.cpp2
-rw-r--r--Userland/Libraries/LibGfx/BMPLoader.cpp2
-rw-r--r--Userland/Libraries/LibGfx/BMPWriter.cpp4
-rw-r--r--Userland/Libraries/LibGfx/Bitmap.cpp2
-rw-r--r--Userland/Libraries/LibGfx/PNGWriter.cpp2
-rw-r--r--Userland/Libraries/LibHTTP/Job.cpp2
-rw-r--r--Userland/Libraries/LibIPC/Connection.cpp6
-rw-r--r--Userland/Libraries/LibIPC/Decoder.cpp9
-rw-r--r--Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp6
-rw-r--r--Userland/Libraries/LibJS/Runtime/ArrayBuffer.h2
-rw-r--r--Userland/Libraries/LibPDF/Filter.cpp9
-rw-r--r--Userland/Libraries/LibPDF/Parser.cpp2
-rw-r--r--Userland/Libraries/LibProtocol/RequestClient.cpp2
-rw-r--r--Userland/Libraries/LibProtocol/WebSocket.cpp4
-rw-r--r--Userland/Libraries/LibSQL/Heap.cpp2
-rw-r--r--Userland/Libraries/LibTLS/HandshakeClient.cpp6
-rw-r--r--Userland/Libraries/LibTLS/HandshakeServer.cpp6
-rw-r--r--Userland/Libraries/LibTLS/Record.cpp16
-rw-r--r--Userland/Libraries/LibTLS/TLSPacketBuilder.h2
-rw-r--r--Userland/Libraries/LibVideo/MatroskaReader.cpp6
-rw-r--r--Userland/Libraries/LibWeb/Bindings/IDLAbstractOperations.cpp6
-rw-r--r--Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp2
-rw-r--r--Userland/Libraries/LibWeb/Loader/Resource.cpp2
-rw-r--r--Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp2
-rw-r--r--Userland/Libraries/LibWebSocket/Message.h2
-rw-r--r--Userland/Libraries/LibWebSocket/WebSocket.cpp6
-rw-r--r--Userland/Services/EchoServer/Client.cpp5
-rw-r--r--Userland/Services/InspectorServer/InspectableProcess.cpp2
-rw-r--r--Userland/Services/RequestServer/HttpCommon.h2
-rw-r--r--Userland/Services/SpiceAgent/SpiceAgent.cpp12
-rw-r--r--Userland/Services/TelnetServer/Client.cpp10
-rw-r--r--Userland/Services/WebServer/Client.cpp4
-rw-r--r--Userland/Shell/AST.cpp4
-rw-r--r--Userland/Utilities/disk_benchmark.cpp4
-rw-r--r--Userland/Utilities/ping.cpp4
-rw-r--r--Userland/Utilities/strace.cpp10
79 files changed, 202 insertions, 274 deletions
diff --git a/AK/Base64.cpp b/AK/Base64.cpp
index f9d321ee38..11b190ec08 100644
--- a/AK/Base64.cpp
+++ b/AK/Base64.cpp
@@ -97,7 +97,7 @@ Optional<ByteBuffer> decode_base64(StringView input)
output.append(out2);
}
- return ByteBuffer::copy(output);
+ return ByteBuffer::copy(output).release_value_but_fixme_should_propagate_errors();
}
String encode_base64(ReadonlyBytes input)
diff --git a/AK/ByteBuffer.h b/AK/ByteBuffer.h
index abf4c9d53b..91617d1bfb 100644
--- a/AK/ByteBuffer.h
+++ b/AK/ByteBuffer.h
@@ -61,35 +61,31 @@ public:
return *this;
}
- [[nodiscard]] static Optional<ByteBuffer> create_uninitialized(size_t size)
+ [[nodiscard]] static ErrorOr<ByteBuffer> create_uninitialized(size_t size)
{
auto buffer = ByteBuffer();
- if (buffer.try_resize(size).is_error())
- return {};
+ TRY(buffer.try_resize(size));
return { move(buffer) };
}
- [[nodiscard]] static Optional<ByteBuffer> create_zeroed(size_t size)
+ [[nodiscard]] static ErrorOr<ByteBuffer> create_zeroed(size_t size)
{
- auto buffer_result = create_uninitialized(size);
- if (!buffer_result.has_value())
- return {};
+ auto buffer = TRY(create_uninitialized(size));
- auto& buffer = buffer_result.value();
buffer.zero_fill();
VERIFY(size == 0 || (buffer[0] == 0 && buffer[size - 1] == 0));
- return buffer_result;
+ return { move(buffer) };
}
- [[nodiscard]] static Optional<ByteBuffer> copy(void const* data, size_t size)
+ [[nodiscard]] static ErrorOr<ByteBuffer> copy(void const* data, size_t size)
{
- auto buffer = create_uninitialized(size);
- if (buffer.has_value() && size != 0)
- __builtin_memcpy(buffer->data(), data, size);
- return buffer;
+ auto buffer = TRY(create_uninitialized(size));
+ if (size != 0)
+ __builtin_memcpy(buffer.data(), data, size);
+ return { move(buffer) };
}
- [[nodiscard]] static Optional<ByteBuffer> copy(ReadonlyBytes bytes)
+ [[nodiscard]] static ErrorOr<ByteBuffer> copy(ReadonlyBytes bytes)
{
return copy(bytes.data(), bytes.size());
}
diff --git a/AK/Hex.cpp b/AK/Hex.cpp
index 7e8cd99d86..dca499f0d8 100644
--- a/AK/Hex.cpp
+++ b/AK/Hex.cpp
@@ -21,7 +21,7 @@ Optional<ByteBuffer> decode_hex(StringView input)
return {};
auto output_result = ByteBuffer::create_zeroed(input.length() / 2);
- if (!output_result.has_value())
+ if (output_result.is_error())
return {};
auto& output = output_result.value();
@@ -38,7 +38,7 @@ Optional<ByteBuffer> decode_hex(StringView input)
output[i] = (c1 << 4) + c2;
}
- return output_result;
+ return output_result.release_value();
}
String encode_hex(const ReadonlyBytes input)
diff --git a/AK/MemoryStream.h b/AK/MemoryStream.h
index 04565932cb..2b58ce1462 100644
--- a/AK/MemoryStream.h
+++ b/AK/MemoryStream.h
@@ -224,7 +224,7 @@ public:
size_t nwritten = 0;
while (bytes.size() - nwritten > 0) {
if ((m_write_offset + nwritten) % chunk_size == 0)
- m_chunks.append(ByteBuffer::create_uninitialized(chunk_size).release_value()); // FIXME: Handle possible OOM situation.
+ m_chunks.append(ByteBuffer::create_uninitialized(chunk_size).release_value_but_fixme_should_propagate_errors()); // FIXME: Handle possible OOM situation.
nwritten += bytes.slice(nwritten).copy_trimmed_to(m_chunks.last().bytes().slice((m_write_offset + nwritten) % chunk_size));
}
@@ -242,7 +242,7 @@ public:
ByteBuffer copy_into_contiguous_buffer() const
{
// FIXME: Handle possible OOM situation.
- auto buffer = ByteBuffer::create_uninitialized(size()).release_value();
+ auto buffer = ByteBuffer::create_uninitialized(size()).release_value_but_fixme_should_propagate_errors();
const auto nread = read_without_consuming(buffer);
VERIFY(nread == buffer.size());
diff --git a/AK/String.cpp b/AK/String.cpp
index b975781de0..3b542df2ec 100644
--- a/AK/String.cpp
+++ b/AK/String.cpp
@@ -177,7 +177,7 @@ ByteBuffer String::to_byte_buffer() const
if (!m_impl)
return {};
// FIXME: Handle OOM failure.
- return ByteBuffer::copy(bytes()).release_value();
+ return ByteBuffer::copy(bytes()).release_value_but_fixme_should_propagate_errors();
}
template<typename T>
diff --git a/AK/StringBuilder.cpp b/AK/StringBuilder.cpp
index ed9c1d1087..9b5f31505a 100644
--- a/AK/StringBuilder.cpp
+++ b/AK/StringBuilder.cpp
@@ -84,7 +84,7 @@ void StringBuilder::appendvf(char const* fmt, va_list ap)
ByteBuffer StringBuilder::to_byte_buffer() const
{
// FIXME: Handle OOM failure.
- return ByteBuffer::copy(data(), length()).release_value();
+ return ByteBuffer::copy(data(), length()).release_value_but_fixme_should_propagate_errors();
}
String StringBuilder::to_string() const
diff --git a/Kernel/FileSystem/BlockBasedFileSystem.cpp b/Kernel/FileSystem/BlockBasedFileSystem.cpp
index 08280206a0..489f534439 100644
--- a/Kernel/FileSystem/BlockBasedFileSystem.cpp
+++ b/Kernel/FileSystem/BlockBasedFileSystem.cpp
@@ -140,10 +140,7 @@ ErrorOr<void> BlockBasedFileSystem::write_block(BlockIndex index, const UserOrKe
// NOTE: We copy the `data` to write into a local buffer before taking the cache lock.
// This makes sure any page faults caused by accessing the data will occur before
// we tie down the cache.
- auto buffered_data_or_error = ByteBuffer::create_uninitialized(count);
- if (!buffered_data_or_error.has_value())
- return ENOMEM;
- auto buffered_data = buffered_data_or_error.release_value();
+ auto buffered_data = TRY(ByteBuffer::create_uninitialized(count));
TRY(data.read(buffered_data.bytes()));
diff --git a/Kernel/FileSystem/Ext2FileSystem.cpp b/Kernel/FileSystem/Ext2FileSystem.cpp
index a1e50ed1ff..0a68dab23a 100644
--- a/Kernel/FileSystem/Ext2FileSystem.cpp
+++ b/Kernel/FileSystem/Ext2FileSystem.cpp
@@ -201,10 +201,7 @@ ErrorOr<void> Ext2FSInode::write_indirect_block(BlockBasedFileSystem::BlockIndex
const auto entries_per_block = EXT2_ADDR_PER_BLOCK(&fs().super_block());
VERIFY(blocks_indices.size() <= entries_per_block);
- auto block_contents_result = ByteBuffer::create_uninitialized(fs().block_size());
- if (!block_contents_result.has_value())
- return ENOMEM;
- auto block_contents = block_contents_result.release_value();
+ auto block_contents = TRY(ByteBuffer::create_uninitialized(fs().block_size()));
OutputMemoryStream stream { block_contents };
auto buffer = UserOrKernelBuffer::for_kernel_buffer(stream.data());
@@ -226,10 +223,7 @@ ErrorOr<void> Ext2FSInode::grow_doubly_indirect_block(BlockBasedFileSystem::Bloc
VERIFY(blocks_indices.size() > old_blocks_length);
VERIFY(blocks_indices.size() <= entries_per_doubly_indirect_block);
- auto block_contents_result = ByteBuffer::create_uninitialized(fs().block_size());
- if (!block_contents_result.has_value())
- return ENOMEM;
- auto block_contents = block_contents_result.release_value();
+ auto block_contents = TRY(ByteBuffer::create_uninitialized(fs().block_size()));
auto* block_as_pointers = (unsigned*)block_contents.data();
OutputMemoryStream stream { block_contents };
auto buffer = UserOrKernelBuffer::for_kernel_buffer(stream.data());
@@ -269,10 +263,7 @@ ErrorOr<void> Ext2FSInode::shrink_doubly_indirect_block(BlockBasedFileSystem::Bl
VERIFY(old_blocks_length >= new_blocks_length);
VERIFY(new_blocks_length <= entries_per_doubly_indirect_block);
- auto block_contents_result = ByteBuffer::create_uninitialized(fs().block_size());
- if (!block_contents_result.has_value())
- return ENOMEM;
- auto block_contents = block_contents_result.release_value();
+ auto block_contents = TRY(ByteBuffer::create_uninitialized(fs().block_size()));
auto* block_as_pointers = (unsigned*)block_contents.data();
auto buffer = UserOrKernelBuffer::for_kernel_buffer(reinterpret_cast<u8*>(block_as_pointers));
TRY(fs().read_block(block, &buffer, fs().block_size()));
@@ -305,10 +296,7 @@ ErrorOr<void> Ext2FSInode::grow_triply_indirect_block(BlockBasedFileSystem::Bloc
VERIFY(blocks_indices.size() > old_blocks_length);
VERIFY(blocks_indices.size() <= entries_per_triply_indirect_block);
- auto block_contents_result = ByteBuffer::create_uninitialized(fs().block_size());
- if (!block_contents_result.has_value())
- return ENOMEM;
- auto block_contents = block_contents_result.release_value();
+ auto block_contents = TRY(ByteBuffer::create_uninitialized(fs().block_size()));
auto* block_as_pointers = (unsigned*)block_contents.data();
OutputMemoryStream stream { block_contents };
auto buffer = UserOrKernelBuffer::for_kernel_buffer(stream.data());
@@ -351,10 +339,7 @@ ErrorOr<void> Ext2FSInode::shrink_triply_indirect_block(BlockBasedFileSystem::Bl
VERIFY(old_blocks_length >= new_blocks_length);
VERIFY(new_blocks_length <= entries_per_triply_indirect_block);
- auto block_contents_result = ByteBuffer::create_uninitialized(fs().block_size());
- if (!block_contents_result.has_value())
- return ENOMEM;
- auto block_contents = block_contents_result.release_value();
+ auto block_contents = TRY(ByteBuffer::create_uninitialized(fs().block_size()));
auto* block_as_pointers = (unsigned*)block_contents.data();
auto buffer = UserOrKernelBuffer::for_kernel_buffer(reinterpret_cast<u8*>(block_as_pointers));
TRY(fs().read_block(block, &buffer, fs().block_size()));
@@ -587,10 +572,7 @@ ErrorOr<Vector<Ext2FS::BlockIndex>> Ext2FSInode::compute_block_list_impl_interna
if (!count)
return {};
size_t read_size = count * sizeof(u32);
- auto maybe_array_storage = ByteBuffer::create_uninitialized(read_size);
- if (!maybe_array_storage.has_value())
- return ENOMEM;
- auto array_storage = maybe_array_storage.release_value();
+ auto array_storage = TRY(ByteBuffer::create_uninitialized(read_size));
auto* array = (u32*)array_storage.data();
auto buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)array);
TRY(fs().read_block(array_block_index, &buffer, read_size, 0));
@@ -1111,10 +1093,7 @@ ErrorOr<void> Ext2FSInode::write_directory(Vector<Ext2FSDirectoryEntry>& entries
dbgln_if(EXT2_DEBUG, "Ext2FSInode[{}]::write_directory(): New directory contents to write (size {}):", identifier(), directory_size);
- auto directory_data_result = ByteBuffer::create_uninitialized(directory_size);
- if (!directory_data_result.has_value())
- return ENOMEM;
- auto directory_data = directory_data_result.release_value();
+ auto directory_data = TRY(ByteBuffer::create_uninitialized(directory_size));
OutputMemoryStream stream { directory_data };
for (auto& entry : entries) {
diff --git a/Kernel/Graphics/Bochs/GraphicsAdapter.cpp b/Kernel/Graphics/Bochs/GraphicsAdapter.cpp
index aee5db05ff..166806d7cb 100644
--- a/Kernel/Graphics/Bochs/GraphicsAdapter.cpp
+++ b/Kernel/Graphics/Bochs/GraphicsAdapter.cpp
@@ -281,10 +281,7 @@ ErrorOr<ByteBuffer> BochsGraphicsAdapter::get_edid(size_t output_port_index) con
if (output_port_index != 0)
return Error::from_errno(ENODEV);
- auto bytes = ByteBuffer::copy(const_cast<u8 const*>(m_registers->edid_data), sizeof(m_registers->edid_data));
- if (!bytes.has_value())
- return Error::from_errno(ENOMEM);
- return bytes.release_value();
+ return ByteBuffer::copy(const_cast<u8 const*>(m_registers->edid_data), sizeof(m_registers->edid_data));
}
}
diff --git a/Kernel/Graphics/Intel/NativeGraphicsAdapter.cpp b/Kernel/Graphics/Intel/NativeGraphicsAdapter.cpp
index 51489e46eb..ba803226b2 100644
--- a/Kernel/Graphics/Intel/NativeGraphicsAdapter.cpp
+++ b/Kernel/Graphics/Intel/NativeGraphicsAdapter.cpp
@@ -661,12 +661,8 @@ ErrorOr<ByteBuffer> IntelNativeGraphicsAdapter::get_edid(size_t output_port_inde
return Error::from_errno(ENODEV);
}
- if (m_crt_edid.has_value()) {
- auto bytes = ByteBuffer::copy(m_crt_edid_bytes, sizeof(m_crt_edid_bytes));
- if (!bytes.has_value())
- return Error::from_errno(ENOMEM);
- return bytes.release_value();
- }
+ if (m_crt_edid.has_value())
+ return ByteBuffer::copy(m_crt_edid_bytes, sizeof(m_crt_edid_bytes));
return ByteBuffer {};
}
diff --git a/Kernel/Graphics/VirtIOGPU/GraphicsAdapter.cpp b/Kernel/Graphics/VirtIOGPU/GraphicsAdapter.cpp
index 6264396d33..27212ff2ed 100644
--- a/Kernel/Graphics/VirtIOGPU/GraphicsAdapter.cpp
+++ b/Kernel/Graphics/VirtIOGPU/GraphicsAdapter.cpp
@@ -214,12 +214,8 @@ ErrorOr<ByteBuffer> GraphicsAdapter::get_edid(size_t output_port_index) const
if (output_port_index >= VIRTIO_GPU_MAX_SCANOUTS)
return Error::from_errno(ENODEV);
auto& edid = m_scanouts[output_port_index].edid;
- if (edid.has_value()) {
- auto bytes = ByteBuffer::copy(edid.value().bytes());
- if (!bytes.has_value())
- return Error::from_errno(ENOMEM);
- return bytes.release_value();
- }
+ if (edid.has_value())
+ return ByteBuffer::copy(edid.value().bytes());
return ByteBuffer {};
}
@@ -243,11 +239,8 @@ auto GraphicsAdapter::query_edid(u32 scanout_id) -> ErrorOr<Optional<EDID::Parse
if (response.size == 0)
return Error::from_string_literal("VirtIO::GraphicsAdapter: Failed to get EDID, empty buffer");
- auto edid_buffer = ByteBuffer::copy(response.edid, response.size);
- if (!edid_buffer.has_value())
- return Error::from_errno(ENOMEM);
-
- auto edid = TRY(EDID::Parser::from_bytes(edid_buffer.release_value()));
+ auto edid_buffer = TRY(ByteBuffer::copy(response.edid, response.size));
+ auto edid = TRY(EDID::Parser::from_bytes(move(edid_buffer)));
return edid;
}
diff --git a/Kernel/Net/NE2000/NetworkAdapter.cpp b/Kernel/Net/NE2000/NetworkAdapter.cpp
index 277a2f7c72..b22622a809 100644
--- a/Kernel/Net/NE2000/NetworkAdapter.cpp
+++ b/Kernel/Net/NE2000/NetworkAdapter.cpp
@@ -421,11 +421,11 @@ void NE2000NetworkAdapter::receive()
u8 drop_buffer[NE2K_PAGE_SIZE];
Bytes buffer { drop_buffer, array_size(drop_buffer) };
bool will_drop { false };
- if (!packet_result.has_value()) {
+ if (packet_result.is_error()) {
dbgln("NE2000NetworkAdapter: Not enough memory for packet with length = {}, dropping.", header.length);
will_drop = true;
} else {
- buffer = packet_result->bytes();
+ buffer = packet_result.value().bytes();
}
int current_offset = 0;
diff --git a/Kernel/Net/NetworkAdapter.cpp b/Kernel/Net/NetworkAdapter.cpp
index e6db69ec95..13afd672d3 100644
--- a/Kernel/Net/NetworkAdapter.cpp
+++ b/Kernel/Net/NetworkAdapter.cpp
@@ -34,11 +34,11 @@ void NetworkAdapter::send(const MACAddress& destination, const ARPPacket& packet
{
size_t size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(ARPPacket);
auto buffer_result = NetworkByteBuffer::create_zeroed(size_in_bytes);
- if (!buffer_result.has_value()) {
+ if (buffer_result.is_error()) {
dbgln("Dropping ARP packet targeted at {} as there is not enough memory to buffer it", packet.target_hardware_address().to_string());
return;
}
- auto* eth = (EthernetFrameHeader*)buffer_result->data();
+ auto* eth = (EthernetFrameHeader*)buffer_result.value().data();
eth->set_source(mac_address());
eth->set_destination(destination);
eth->set_ether_type(EtherType::ARP);
diff --git a/Kernel/Random.h b/Kernel/Random.h
index e5f4a61fd6..ea4e25a7b3 100644
--- a/Kernel/Random.h
+++ b/Kernel/Random.h
@@ -29,9 +29,9 @@ public:
using HashType = HashT;
using DigestType = typename HashT::DigestType;
- // FIXME: Do something other than VERIFY()'ing inside Optional in case of OOM.
+ // FIXME: Do something other than VERIFY()'ing in case of OOM.
FortunaPRNG()
- : m_counter(ByteBuffer::create_zeroed(BlockType::block_size()).release_value())
+ : m_counter(ByteBuffer::create_zeroed(BlockType::block_size()).release_value_but_fixme_should_propagate_errors())
{
}
@@ -101,7 +101,7 @@ private:
} else {
auto buffer_result = ByteBuffer::copy(digest.immutable_data(), digest.data_length());
// If there's no memory left to copy this into, bail out.
- if (!buffer_result.has_value())
+ if (buffer_result.is_error())
return;
m_key = buffer_result.release_value();
diff --git a/Kernel/Storage/ATA/IDEChannel.cpp b/Kernel/Storage/ATA/IDEChannel.cpp
index c394951436..c23c7d7647 100644
--- a/Kernel/Storage/ATA/IDEChannel.cpp
+++ b/Kernel/Storage/ATA/IDEChannel.cpp
@@ -381,8 +381,8 @@ UNMAP_AFTER_INIT void IDEChannel::detect_disks()
}
// FIXME: Handle possible OOM situation here.
- ByteBuffer wbuf = ByteBuffer::create_uninitialized(512).release_value();
- ByteBuffer bbuf = ByteBuffer::create_uninitialized(512).release_value();
+ ByteBuffer wbuf = ByteBuffer::create_uninitialized(512).release_value_but_fixme_should_propagate_errors();
+ ByteBuffer bbuf = ByteBuffer::create_uninitialized(512).release_value_but_fixme_should_propagate_errors();
u8* b = bbuf.data();
u16* w = (u16*)wbuf.data();
diff --git a/Kernel/Storage/NVMe/NVMeController.cpp b/Kernel/Storage/NVMe/NVMeController.cpp
index 4c3a28499e..15e4e9a926 100644
--- a/Kernel/Storage/NVMe/NVMeController.cpp
+++ b/Kernel/Storage/NVMe/NVMeController.cpp
@@ -151,7 +151,7 @@ UNMAP_AFTER_INIT ErrorOr<void> NVMeController::identify_and_init_namespaces()
RefPtr<Memory::PhysicalPage> prp_dma_buffer;
OwnPtr<Memory::Region> prp_dma_region;
- auto namespace_data_struct = ByteBuffer::create_zeroed(NVMe_IDENTIFY_SIZE).release_value();
+ auto namespace_data_struct = TRY(ByteBuffer::create_zeroed(NVMe_IDENTIFY_SIZE));
u32 active_namespace_list[NVMe_IDENTIFY_SIZE / sizeof(u32)];
{
diff --git a/Kernel/Storage/Partition/GUIDPartitionTable.cpp b/Kernel/Storage/Partition/GUIDPartitionTable.cpp
index f9c99bd0f4..d85e855857 100644
--- a/Kernel/Storage/Partition/GUIDPartitionTable.cpp
+++ b/Kernel/Storage/Partition/GUIDPartitionTable.cpp
@@ -59,7 +59,7 @@ GUIDPartitionTable::GUIDPartitionTable(const StorageDevice& device)
: MBRPartitionTable(device)
{
// FIXME: Handle OOM failure here.
- m_cached_header = ByteBuffer::create_zeroed(m_device->block_size()).release_value();
+ m_cached_header = ByteBuffer::create_zeroed(m_device->block_size()).release_value_but_fixme_should_propagate_errors();
VERIFY(partitions_count() == 0);
if (!initialize())
m_valid = false;
@@ -89,7 +89,7 @@ bool GUIDPartitionTable::initialize()
}
auto entries_buffer_result = ByteBuffer::create_zeroed(m_device->block_size());
- if (!entries_buffer_result.has_value()) {
+ if (entries_buffer_result.is_error()) {
dbgln("GUIPartitionTable: not enough memory for entries buffer");
return false;
}
diff --git a/Kernel/Storage/Partition/MBRPartitionTable.cpp b/Kernel/Storage/Partition/MBRPartitionTable.cpp
index 3148b644bc..4b6221412a 100644
--- a/Kernel/Storage/Partition/MBRPartitionTable.cpp
+++ b/Kernel/Storage/Partition/MBRPartitionTable.cpp
@@ -47,7 +47,7 @@ bool MBRPartitionTable::read_boot_record()
MBRPartitionTable::MBRPartitionTable(const StorageDevice& device, u32 start_lba)
: PartitionTable(device)
, m_start_lba(start_lba)
- , m_cached_header(ByteBuffer::create_zeroed(m_device->block_size()).release_value()) // FIXME: Do something sensible if this fails because of OOM.
+ , m_cached_header(ByteBuffer::create_zeroed(m_device->block_size()).release_value_but_fixme_should_propagate_errors()) // FIXME: Do something sensible if this fails because of OOM.
{
if (!read_boot_record() || !initialize())
return;
@@ -68,7 +68,7 @@ MBRPartitionTable::MBRPartitionTable(const StorageDevice& device, u32 start_lba)
MBRPartitionTable::MBRPartitionTable(const StorageDevice& device)
: PartitionTable(device)
, m_start_lba(0)
- , m_cached_header(ByteBuffer::create_zeroed(m_device->block_size()).release_value()) // FIXME: Do something sensible if this fails because of OOM.
+ , m_cached_header(ByteBuffer::create_zeroed(m_device->block_size()).release_value_but_fixme_should_propagate_errors()) // FIXME: Do something sensible if this fails because of OOM.
{
if (!read_boot_record() || contains_ebr() || is_protective_mbr() || !initialize())
return;
diff --git a/Kernel/Storage/StorageDevice.cpp b/Kernel/Storage/StorageDevice.cpp
index dc126b379b..4624f6c489 100644
--- a/Kernel/Storage/StorageDevice.cpp
+++ b/Kernel/Storage/StorageDevice.cpp
@@ -62,10 +62,7 @@ ErrorOr<size_t> StorageDevice::read(OpenFileDescription&, u64 offset, UserOrKern
off_t pos = whole_blocks * block_size();
if (remaining > 0) {
- auto data_result = ByteBuffer::create_uninitialized(block_size());
- if (!data_result.has_value())
- return ENOMEM;
- auto data = data_result.release_value();
+ auto data = TRY(ByteBuffer::create_uninitialized(block_size()));
auto data_buffer = UserOrKernelBuffer::for_kernel_buffer(data.data());
auto read_request = TRY(try_make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index + whole_blocks, 1, data_buffer, block_size()));
auto result = read_request->wait();
@@ -133,7 +130,7 @@ ErrorOr<size_t> StorageDevice::write(OpenFileDescription&, u64 offset, const Use
// then write the whole block back to the disk.
if (remaining > 0) {
// FIXME: Do something sensible with this OOM scenario.
- auto data = ByteBuffer::create_zeroed(block_size()).release_value();
+ auto data = ByteBuffer::create_zeroed(block_size()).release_value_but_fixme_should_propagate_errors();
auto data_buffer = UserOrKernelBuffer::for_kernel_buffer(data.data());
{
diff --git a/Tests/AK/TestByteBuffer.cpp b/Tests/AK/TestByteBuffer.cpp
index 1e11155809..010972430e 100644
--- a/Tests/AK/TestByteBuffer.cpp
+++ b/Tests/AK/TestByteBuffer.cpp
@@ -43,8 +43,8 @@ TEST_CASE(equality_operator)
#if COMPILE_NEGATIVE_TESTS
TEST_CASE(negative_operator_lt)
{
- ByteBuffer a = ByteBuffer::copy("Hello, world", 10);
- ByteBuffer b = ByteBuffer::copy("Hello, friend", 10);
+ ByteBuffer a = ByteBuffer::copy("Hello, world", 10).release_value();
+ ByteBuffer b = ByteBuffer::copy("Hello, friend", 10).release_value();
[[maybe_unused]] auto res = a < b;
// error: error: use of deleted function ‘bool AK::ByteBuffer::operator<(const AK::ByteBuffer&) const’
}
diff --git a/Tests/Kernel/stress-writeread.cpp b/Tests/Kernel/stress-writeread.cpp
index 62cc02ada7..6ae5bf5c46 100644
--- a/Tests/Kernel/stress-writeread.cpp
+++ b/Tests/Kernel/stress-writeread.cpp
@@ -85,7 +85,7 @@ int main(int argc, char** argv)
args_parser.parse(argc, argv);
auto buffer_result = AK::ByteBuffer::create_zeroed(block_size);
- if (!buffer_result.has_value()) {
+ if (buffer_result.is_error()) {
warnln("Failed to allocate a buffer of {} bytes", block_size);
return EXIT_FAILURE;
}
diff --git a/Tests/LibCore/TestLibCoreStream.cpp b/Tests/LibCore/TestLibCoreStream.cpp
index 9447e79b85..2352a196e0 100644
--- a/Tests/LibCore/TestLibCoreStream.cpp
+++ b/Tests/LibCore/TestLibCoreStream.cpp
@@ -61,7 +61,7 @@ TEST_CASE(file_read_bytes)
auto file = maybe_file.release_value();
auto maybe_buffer = ByteBuffer::create_uninitialized(131);
- EXPECT(maybe_buffer.has_value());
+ EXPECT(!maybe_buffer.is_error());
auto buffer = maybe_buffer.release_value();
auto result = file->read(buffer);
@@ -85,7 +85,7 @@ TEST_CASE(file_seeking_around)
EXPECT_EQ(file->size().release_value(), 8702);
auto maybe_buffer = ByteBuffer::create_uninitialized(16);
- EXPECT(maybe_buffer.has_value());
+ EXPECT(!maybe_buffer.is_error());
auto buffer = maybe_buffer.release_value();
StringView buffer_contents { buffer.bytes() };
@@ -118,7 +118,7 @@ TEST_CASE(file_adopt_fd)
EXPECT_EQ(file->size().release_value(), 8702);
auto maybe_buffer = ByteBuffer::create_uninitialized(16);
- EXPECT(maybe_buffer.has_value());
+ EXPECT(!maybe_buffer.is_error());
auto buffer = maybe_buffer.release_value();
StringView buffer_contents { buffer.bytes() };
@@ -182,6 +182,7 @@ TEST_CASE(tcp_socket_read)
EXPECT_EQ(client_socket->pending_bytes().release_value(), sent_data.length());
auto maybe_receive_buffer = ByteBuffer::create_uninitialized(64);
+ EXPECT(!maybe_receive_buffer.is_error());
auto receive_buffer = maybe_receive_buffer.release_value();
auto maybe_nread = client_socket->read(receive_buffer);
EXPECT(!maybe_nread.is_error());
@@ -213,7 +214,9 @@ TEST_CASE(tcp_socket_write)
EXPECT(client_socket->write_or_error({ sent_data.characters_without_null_termination(), sent_data.length() }));
client_socket->close();
- auto receive_buffer = ByteBuffer::create_uninitialized(64).release_value();
+ auto maybe_receive_buffer = ByteBuffer::create_uninitialized(64);
+ EXPECT(!maybe_receive_buffer.is_error());
+ auto receive_buffer = maybe_receive_buffer.release_value();
auto maybe_nread = server_socket->read(receive_buffer);
EXPECT(!maybe_nread.is_error());
auto nread = maybe_nread.release_value();
@@ -248,6 +251,7 @@ TEST_CASE(tcp_socket_eof)
EXPECT_EQ(client_socket->pending_bytes().release_value(), 0ul);
auto maybe_receive_buffer = ByteBuffer::create_uninitialized(1);
+ EXPECT(!maybe_receive_buffer.is_error());
auto receive_buffer = maybe_receive_buffer.release_value();
EXPECT_EQ(client_socket->read(receive_buffer).release_value(), 0ul);
EXPECT(client_socket->is_eof());
@@ -294,6 +298,7 @@ TEST_CASE(udp_socket_read_write)
EXPECT_EQ(client_socket->read(small_buffer).error().code(), EMSGSIZE);
auto maybe_client_receive_buffer = ByteBuffer::create_uninitialized(64);
+ EXPECT(!maybe_client_receive_buffer.is_error());
auto client_receive_buffer = maybe_client_receive_buffer.release_value();
auto maybe_nread = client_socket->read(client_receive_buffer);
EXPECT(!maybe_nread.is_error());
@@ -337,6 +342,7 @@ TEST_CASE(local_socket_read)
EXPECT_EQ(client_socket->pending_bytes().release_value(), sent_data.length());
auto maybe_receive_buffer = ByteBuffer::create_uninitialized(64);
+ EXPECT(!maybe_receive_buffer.is_error());
auto receive_buffer = maybe_receive_buffer.release_value();
auto maybe_nread = client_socket->read(receive_buffer);
EXPECT(!maybe_nread.is_error());
@@ -366,7 +372,9 @@ TEST_CASE(local_socket_write)
EXPECT(MUST(server_socket->can_read_without_blocking(100)));
auto pending_bytes = MUST(server_socket->pending_bytes());
- auto receive_buffer = ByteBuffer::create_uninitialized(pending_bytes).release_value();
+ auto maybe_receive_buffer = ByteBuffer::create_uninitialized(pending_bytes);
+ EXPECT(!maybe_receive_buffer.is_error());
+ auto receive_buffer = maybe_receive_buffer.release_value();
auto maybe_nread = server_socket->read(receive_buffer);
EXPECT(!maybe_nread.is_error());
EXPECT_EQ(maybe_nread.value(), sent_data.length());
diff --git a/Tests/LibIMAP/TestQuotedPrintable.cpp b/Tests/LibIMAP/TestQuotedPrintable.cpp
index 3f1040a0eb..e89c5b06f3 100644
--- a/Tests/LibIMAP/TestQuotedPrintable.cpp
+++ b/Tests/LibIMAP/TestQuotedPrintable.cpp
@@ -17,7 +17,7 @@ TEST_CASE(test_decode)
auto decode_equal_byte_buffer = [](const char* input, const char* expected, size_t expected_length) {
auto decoded = IMAP::decode_quoted_printable(StringView(input));
- EXPECT(decoded == *ByteBuffer::copy(expected, expected_length));
+ EXPECT(decoded == ByteBuffer::copy(expected, expected_length).value());
};
decode_equal("hello world", "hello world");
diff --git a/Userland/Applications/Browser/BrowserWindow.cpp b/Userland/Applications/Browser/BrowserWindow.cpp
index dfcb6188cd..99607fb094 100644
--- a/Userland/Applications/Browser/BrowserWindow.cpp
+++ b/Userland/Applications/Browser/BrowserWindow.cpp
@@ -387,11 +387,7 @@ ErrorOr<void> BrowserWindow::load_search_engines(GUI::Menu& settings_menu)
auto search_engines_file = TRY(Core::Stream::File::open(Browser::search_engines_file_path(), Core::Stream::OpenMode::Read));
auto file_size = TRY(search_engines_file->size());
- auto maybe_buffer = ByteBuffer::create_uninitialized(file_size);
- if (!maybe_buffer.has_value())
- return Error::from_string_literal("Unable to allocate buffer for reading search-engines file.");
-
- auto buffer = maybe_buffer.release_value();
+ auto buffer = TRY(ByteBuffer::create_uninitialized(file_size));
if (search_engines_file->read_or_error(buffer)) {
StringView buffer_contents { buffer.bytes() };
if (auto json = TRY(JsonValue::from_string(buffer_contents)); json.is_array()) {
diff --git a/Userland/Applications/Browser/main.cpp b/Userland/Applications/Browser/main.cpp
index 64d118264d..413bb68059 100644
--- a/Userland/Applications/Browser/main.cpp
+++ b/Userland/Applications/Browser/main.cpp
@@ -36,17 +36,13 @@ static ErrorOr<void> load_content_filters()
{
auto file = TRY(Core::Stream::File::open(String::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory()), Core::Stream::OpenMode::Read));
auto ad_filter_list = TRY(Core::Stream::BufferedFile::create(move(file)));
- auto maybe_buffer = ByteBuffer::create_uninitialized(4096);
- if (maybe_buffer.has_value()) {
- auto buffer = maybe_buffer.release_value();
-
- while (TRY(ad_filter_list->can_read_line())) {
- auto length = TRY(ad_filter_list->read_line(buffer));
- StringView line { buffer.data(), length };
- dbgln("Content filter for {}", line);
- if (!line.is_empty())
- Browser::g_content_filters.append(line);
- }
+ auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
+ while (TRY(ad_filter_list->can_read_line())) {
+ auto length = TRY(ad_filter_list->read_line(buffer));
+ StringView line { buffer.data(), length };
+ dbgln("Content filter for {}", line);
+ if (!line.is_empty())
+ Browser::g_content_filters.append(line);
}
return {};
diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp
index e6da87403e..26bf9b2327 100644
--- a/Userland/Applications/HexEditor/HexEditor.cpp
+++ b/Userland/Applications/HexEditor/HexEditor.cpp
@@ -28,7 +28,7 @@
HexEditor::HexEditor()
: m_blink_timer(Core::Timer::construct())
- , m_document(make<HexDocumentMemory>(ByteBuffer::create_zeroed(0).release_value()))
+ , m_document(make<HexDocumentMemory>(ByteBuffer::create_zeroed(0).release_value_but_fixme_should_propagate_errors()))
{
set_should_hide_unnecessary_scrollbars(true);
set_focus_policy(GUI::FocusPolicy::StrongFocus);
@@ -60,7 +60,7 @@ void HexEditor::set_readonly(bool readonly)
bool HexEditor::open_new_file(size_t size)
{
auto maybe_buffer = ByteBuffer::create_zeroed(size);
- if (!maybe_buffer.has_value()) {
+ if (maybe_buffer.is_error()) {
return false;
}
diff --git a/Userland/Applications/Spreadsheet/ExportDialog.cpp b/Userland/Applications/Spreadsheet/ExportDialog.cpp
index 680d770e66..d16c1b55dc 100644
--- a/Userland/Applications/Spreadsheet/ExportDialog.cpp
+++ b/Userland/Applications/Spreadsheet/ExportDialog.cpp
@@ -36,7 +36,7 @@ CSVExportDialogPage::CSVExportDialogPage(const Sheet& sheet)
m_headers.extend(m_data.take_first());
auto temp_template = String::formatted("{}/spreadsheet-csv-export.{}.XXXXXX", Core::StandardPaths::tempfile_directory(), getpid());
- auto temp_path = ByteBuffer::create_uninitialized(temp_template.length() + 1).release_value();
+ auto temp_path = ByteBuffer::create_uninitialized(temp_template.length() + 1).release_value_but_fixme_should_propagate_errors();
auto buf = reinterpret_cast<char*>(temp_path.data());
auto copy_ok = temp_template.copy_characters_to_buffer(buf, temp_path.size());
VERIFY(copy_ok);
diff --git a/Userland/DevTools/UserspaceEmulator/Emulator_syscalls.cpp b/Userland/DevTools/UserspaceEmulator/Emulator_syscalls.cpp
index b4f05f16f3..569ef6df1a 100644
--- a/Userland/DevTools/UserspaceEmulator/Emulator_syscalls.cpp
+++ b/Userland/DevTools/UserspaceEmulator/Emulator_syscalls.cpp
@@ -458,7 +458,7 @@ int Emulator::virt$setsockopt(FlatPtr params_addr)
if (params.option == SO_RCVTIMEO || params.option == SO_TIMESTAMP) {
auto host_value_buffer_result = ByteBuffer::create_zeroed(params.value_size);
- if (!host_value_buffer_result.has_value())
+ if (host_value_buffer_result.is_error())
return -ENOMEM;
auto& host_value_buffer = host_value_buffer_result.value();
mmu().copy_from_vm(host_value_buffer.data(), (FlatPtr)params.value, params.value_size);
@@ -592,7 +592,7 @@ int Emulator::virt$get_process_name(FlatPtr buffer, int size)
if (size < 0)
return -EINVAL;
auto host_buffer_result = ByteBuffer::create_zeroed((size_t)size);
- if (!host_buffer_result.has_value())
+ if (host_buffer_result.is_error())
return -ENOMEM;
auto& host_buffer = host_buffer_result.value();
int rc = syscall(SC_get_process_name, host_buffer.data(), host_buffer.size());
@@ -635,7 +635,7 @@ int Emulator::virt$recvmsg(int sockfd, FlatPtr msg_addr, int flags)
Vector<iovec, 1> iovs;
for (const auto& iov : mmu_iovs) {
auto buffer_result = ByteBuffer::create_uninitialized(iov.iov_len);
- if (!buffer_result.has_value())
+ if (buffer_result.is_error())
return -ENOMEM;
buffers.append(buffer_result.release_value());
iovs.append({ buffers.last().data(), buffers.last().size() });
@@ -644,7 +644,7 @@ int Emulator::virt$recvmsg(int sockfd, FlatPtr msg_addr, int flags)
ByteBuffer control_buffer;
if (mmu_msg.msg_control) {
auto buffer_result = ByteBuffer::create_uninitialized(mmu_msg.msg_controllen);
- if (!buffer_result.has_value())
+ if (buffer_result.is_error())
return -ENOMEM;
control_buffer = buffer_result.release_value();
}
@@ -686,7 +686,7 @@ int Emulator::virt$sendmsg(int sockfd, FlatPtr msg_addr, int flags)
ByteBuffer control_buffer;
if (mmu_msg.msg_control) {
auto buffer_result = ByteBuffer::create_uninitialized(mmu_msg.msg_controllen);
- if (!buffer_result.has_value())
+ if (buffer_result.is_error())
return -ENOMEM;
control_buffer = buffer_result.release_value();
}
@@ -770,7 +770,7 @@ int Emulator::virt$getgroups(ssize_t count, FlatPtr groups)
return syscall(SC_getgroups, 0, nullptr);
auto buffer_result = ByteBuffer::create_uninitialized(count * sizeof(gid_t));
- if (!buffer_result.has_value())
+ if (buffer_result.is_error())
return -ENOMEM;
auto& buffer = buffer_result.value();
int rc = syscall(SC_getgroups, count, buffer.data());
@@ -905,7 +905,7 @@ u32 Emulator::virt$mmap(u32 params_addr)
String name_str;
if (params.name.characters) {
auto buffer_result = ByteBuffer::create_uninitialized(params.name.length);
- if (!buffer_result.has_value())
+ if (buffer_result.is_error())
return -ENOMEM;
auto& name = buffer_result.value();
mmu().copy_from_vm(name.data(), (FlatPtr)params.name.characters, params.name.length);
@@ -1052,7 +1052,7 @@ u32 Emulator::virt$read(int fd, FlatPtr buffer, ssize_t size)
if (size < 0)
return -EINVAL;
auto buffer_result = ByteBuffer::create_uninitialized(size);
- if (!buffer_result.has_value())
+ if (buffer_result.is_error())
return -ENOMEM;
auto& local_buffer = buffer_result.value();
int nread = syscall(SC_read, fd, local_buffer.data(), local_buffer.size());
@@ -1082,7 +1082,7 @@ void Emulator::virt$exit(int status)
ssize_t Emulator::virt$getrandom(FlatPtr buffer, size_t buffer_size, unsigned int flags)
{
auto buffer_result = ByteBuffer::create_uninitialized(buffer_size);
- if (!buffer_result.has_value())
+ if (buffer_result.is_error())
return -ENOMEM;
auto& host_buffer = buffer_result.value();
int rc = syscall(SC_getrandom, host_buffer.data(), host_buffer.size(), flags);
@@ -1095,7 +1095,7 @@ ssize_t Emulator::virt$getrandom(FlatPtr buffer, size_t buffer_size, unsigned in
int Emulator::virt$get_dir_entries(int fd, FlatPtr buffer, ssize_t size)
{
auto buffer_result = ByteBuffer::create_uninitialized(size);
- if (!buffer_result.has_value())
+ if (buffer_result.is_error())
return -ENOMEM;
auto& host_buffer = buffer_result.value();
int rc = syscall(SC_get_dir_entries, fd, host_buffer.data(), host_buffer.size());
@@ -1279,7 +1279,7 @@ int Emulator::virt$realpath(FlatPtr params_addr)
auto path = mmu().copy_buffer_from_vm((FlatPtr)params.path.characters, params.path.length);
auto buffer_result = ByteBuffer::create_zeroed(params.buffer.size);
- if (!buffer_result.has_value())
+ if (buffer_result.is_error())
return -ENOMEM;
auto& host_buffer = buffer_result.value();
@@ -1298,7 +1298,7 @@ int Emulator::virt$gethostname(FlatPtr buffer, ssize_t buffer_size)
if (buffer_size < 0)
return -EINVAL;
auto buffer_result = ByteBuffer::create_zeroed(buffer_size);
- if (!buffer_result.has_value())
+ if (buffer_result.is_error())
return -ENOMEM;
auto& host_buffer = buffer_result.value();
int rc = syscall(SC_gethostname, host_buffer.data(), host_buffer.size());
@@ -1386,7 +1386,7 @@ int Emulator::virt$setpgid(pid_t pid, pid_t pgid)
int Emulator::virt$ttyname(int fd, FlatPtr buffer, size_t buffer_size)
{
auto buffer_result = ByteBuffer::create_zeroed(buffer_size);
- if (!buffer_result.has_value())
+ if (buffer_result.is_error())
return -ENOMEM;
auto& host_buffer = buffer_result.value();
int rc = syscall(SC_ttyname, fd, host_buffer.data(), host_buffer.size());
@@ -1399,7 +1399,7 @@ int Emulator::virt$ttyname(int fd, FlatPtr buffer, size_t buffer_size)
int Emulator::virt$getcwd(FlatPtr buffer, size_t buffer_size)
{
auto buffer_result = ByteBuffer::create_zeroed(buffer_size);
- if (!buffer_result.has_value())
+ if (buffer_result.is_error())
return -ENOMEM;
auto& host_buffer = buffer_result.value();
int rc = syscall(SC_getcwd, host_buffer.data(), host_buffer.size());
@@ -1528,7 +1528,7 @@ int Emulator::virt$readlink(FlatPtr params_addr)
auto path = mmu().copy_buffer_from_vm((FlatPtr)params.path.characters, params.path.length);
auto buffer_result = ByteBuffer::create_zeroed(params.buffer.size);
- if (!buffer_result.has_value())
+ if (buffer_result.is_error())
return -ENOMEM;
auto& host_buffer = buffer_result.value();
diff --git a/Userland/DevTools/UserspaceEmulator/SoftMMU.cpp b/Userland/DevTools/UserspaceEmulator/SoftMMU.cpp
index 62009396d4..e1017a3a88 100644
--- a/Userland/DevTools/UserspaceEmulator/SoftMMU.cpp
+++ b/Userland/DevTools/UserspaceEmulator/SoftMMU.cpp
@@ -321,7 +321,7 @@ void SoftMMU::copy_from_vm(void* destination, const FlatPtr source, size_t size)
ByteBuffer SoftMMU::copy_buffer_from_vm(const FlatPtr source, size_t size)
{
- auto buffer = ByteBuffer::create_uninitialized(size).release_value(); // FIXME: Handle possible OOM situation.
+ auto buffer = ByteBuffer::create_uninitialized(size).release_value_but_fixme_should_propagate_errors(); // FIXME: Handle possible OOM situation.
copy_from_vm(buffer.data(), source, size);
return buffer;
}
diff --git a/Userland/Libraries/LibAudio/FlacLoader.cpp b/Userland/Libraries/LibAudio/FlacLoader.cpp
index dbf498ba7b..c2b0944c56 100644
--- a/Userland/Libraries/LibAudio/FlacLoader.cpp
+++ b/Userland/Libraries/LibAudio/FlacLoader.cpp
@@ -144,7 +144,7 @@ ErrorOr<FlacRawMetadataBlock, LoaderError> FlacLoaderPlugin::next_meta_block(Big
u32 block_length = LOADER_TRY(bit_input.read_bits<u32>(24));
m_data_start_location += 3;
auto block_data_result = ByteBuffer::create_uninitialized(block_length);
- FLAC_VERIFY(block_data_result.has_value(), LoaderError::Category::IO, "Out of memory");
+ FLAC_VERIFY(!block_data_result.is_error(), LoaderError::Category::IO, "Out of memory");
auto block_data = block_data_result.release_value();
// Reads exactly the bytes necessary into the Bytes container
LOADER_TRY(bit_input.read(block_data));
diff --git a/Userland/Libraries/LibAudio/WavLoader.cpp b/Userland/Libraries/LibAudio/WavLoader.cpp
index a562f451ba..69a556cb6d 100644
--- a/Userland/Libraries/LibAudio/WavLoader.cpp
+++ b/Userland/Libraries/LibAudio/WavLoader.cpp
@@ -71,7 +71,7 @@ LoaderSamples WavLoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_in
pcm_bits_per_sample(m_sample_format), sample_format_name(m_sample_format));
auto sample_data_result = ByteBuffer::create_zeroed(bytes_to_read);
- if (!sample_data_result.has_value())
+ if (sample_data_result.is_error())
return LoaderError { LoaderError::Category::IO, static_cast<size_t>(m_loaded_samples), "Couldn't allocate sample buffer" };
auto sample_data = sample_data_result.release_value();
m_stream->read_or_error(sample_data.bytes());
diff --git a/Userland/Libraries/LibCore/IODevice.cpp b/Userland/Libraries/LibCore/IODevice.cpp
index 1911c8936e..889e49a5c0 100644
--- a/Userland/Libraries/LibCore/IODevice.cpp
+++ b/Userland/Libraries/LibCore/IODevice.cpp
@@ -49,7 +49,7 @@ ByteBuffer IODevice::read(size_t max_size)
auto size = min(max_size, m_buffered_data.size());
auto buffer_result = ByteBuffer::create_uninitialized(size);
- if (!buffer_result.has_value()) {
+ if (buffer_result.is_error()) {
dbgln("IODevice::read: Not enough memory to allocate a buffer of {} bytes", size);
return {};
}
@@ -149,7 +149,7 @@ ByteBuffer IODevice::read_all()
}
auto result = ByteBuffer::copy(data);
- if (result.has_value())
+ if (!result.is_error())
return result.release_value();
set_error(ENOMEM);
@@ -174,7 +174,7 @@ String IODevice::read_line(size_t max_size)
return line;
}
auto line_result = ByteBuffer::create_uninitialized(max_size + 1);
- if (!line_result.has_value()) {
+ if (line_result.is_error()) {
dbgln("IODevice::read_line: Not enough memory to allocate a buffer of {} bytes", max_size + 1);
return {};
}
@@ -202,7 +202,7 @@ bool IODevice::populate_read_buffer(size_t size) const
return false;
auto buffer_result = ByteBuffer::create_uninitialized(size);
- if (!buffer_result.has_value()) {
+ if (buffer_result.is_error()) {
dbgln("IODevice::populate_read_buffer: Not enough memory to allocate a buffer of {} bytes", size);
return {};
}
diff --git a/Userland/Libraries/LibCore/InputBitStream.h b/Userland/Libraries/LibCore/InputBitStream.h
index 0e844dc107..573f460dbf 100644
--- a/Userland/Libraries/LibCore/InputBitStream.h
+++ b/Userland/Libraries/LibCore/InputBitStream.h
@@ -99,13 +99,9 @@ public:
m_current_byte.clear();
}
} else {
- // FIXME: This returns Optional so TRY is not useable
- auto temp_buffer = ByteBuffer::create_uninitialized(1);
- if (!temp_buffer.has_value())
- return Error::from_string_literal("Couldn't allocate temporary byte buffer"sv);
-
- TRY(m_stream.read(temp_buffer->bytes()));
- m_current_byte = (*temp_buffer)[0];
+ auto temp_buffer = TRY(ByteBuffer::create_uninitialized(1));
+ TRY(m_stream.read(temp_buffer.bytes()));
+ m_current_byte = temp_buffer[0];
m_bit_offset = 0;
}
}
diff --git a/Userland/Libraries/LibCore/SecretString.cpp b/Userland/Libraries/LibCore/SecretString.cpp
index 4763936a81..493427d423 100644
--- a/Userland/Libraries/LibCore/SecretString.cpp
+++ b/Userland/Libraries/LibCore/SecretString.cpp
@@ -12,14 +12,13 @@ namespace Core {
SecretString SecretString::take_ownership(char*& cstring, size_t length)
{
- auto buffer = ByteBuffer::copy(cstring, length);
- VERIFY(buffer.has_value());
+ auto buffer = ByteBuffer::copy(cstring, length).release_value_but_fixme_should_propagate_errors();
secure_zero(cstring, length);
free(cstring);
cstring = nullptr;
- return SecretString(buffer.release_value());
+ return SecretString(move(buffer));
}
SecretString SecretString::take_ownership(ByteBuffer&& buffer)
diff --git a/Userland/Libraries/LibCore/Stream.h b/Userland/Libraries/LibCore/Stream.h
index 241a324d6c..9bfe2c4b81 100644
--- a/Userland/Libraries/LibCore/Stream.h
+++ b/Userland/Libraries/LibCore/Stream.h
@@ -479,11 +479,9 @@ public:
if (!stream->is_open())
return Error::from_errno(ENOTCONN);
- auto maybe_buffer = ByteBuffer::create_uninitialized(buffer_size);
- if (!maybe_buffer.has_value())
- return Error::from_errno(ENOMEM);
+ auto buffer = TRY(ByteBuffer::create_uninitialized(buffer_size));
- return adopt_nonnull_own_or_enomem(new BufferedType<T>(move(stream), maybe_buffer.release_value()));
+ return adopt_nonnull_own_or_enomem(new BufferedType<T>(move(stream), move(buffer)));
}
T& stream() { return *m_stream; }
diff --git a/Userland/Libraries/LibCore/UDPServer.cpp b/Userland/Libraries/LibCore/UDPServer.cpp
index 9d754d2855..47a07fdf07 100644
--- a/Userland/Libraries/LibCore/UDPServer.cpp
+++ b/Userland/Libraries/LibCore/UDPServer.cpp
@@ -64,7 +64,7 @@ bool UDPServer::bind(const IPv4Address& address, u16 port)
ByteBuffer UDPServer::receive(size_t size, sockaddr_in& in)
{
// FIXME: Handle possible OOM situation.
- auto buf = ByteBuffer::create_uninitialized(size).release_value();
+ auto buf = ByteBuffer::create_uninitialized(size).release_value_but_fixme_should_propagate_errors();
socklen_t in_len = sizeof(in);
ssize_t rlen = ::recvfrom(m_fd, buf.data(), size, 0, (sockaddr*)&in, &in_len);
if (rlen < 0) {
diff --git a/Userland/Libraries/LibCoredump/Reader.cpp b/Userland/Libraries/LibCoredump/Reader.cpp
index 30273b13ed..94f9f75bd6 100644
--- a/Userland/Libraries/LibCoredump/Reader.cpp
+++ b/Userland/Libraries/LibCoredump/Reader.cpp
@@ -63,9 +63,14 @@ Reader::Reader(ReadonlyBytes coredump_bytes)
Optional<ByteBuffer> Reader::decompress_coredump(ReadonlyBytes raw_coredump)
{
auto decompressed_coredump = Compress::GzipDecompressor::decompress_all(raw_coredump);
- if (!decompressed_coredump.has_value())
- return ByteBuffer::copy(raw_coredump); // if we didn't manage to decompress it, try and parse it as decompressed coredump
- return decompressed_coredump;
+ if (decompressed_coredump.has_value())
+ return decompressed_coredump;
+
+ // If we didn't manage to decompress it, try and parse it as decompressed coredump
+ auto bytebuffer = ByteBuffer::copy(raw_coredump);
+ if (bytebuffer.is_error())
+ return {};
+ return bytebuffer.release_value();
}
Reader::~Reader()
diff --git a/Userland/Libraries/LibCrypto/Cipher/Mode/GCM.h b/Userland/Libraries/LibCrypto/Cipher/Mode/GCM.h
index 900fd8b0b7..d5cb048ba9 100644
--- a/Userland/Libraries/LibCrypto/Cipher/Mode/GCM.h
+++ b/Userland/Libraries/LibCrypto/Cipher/Mode/GCM.h
@@ -68,12 +68,12 @@ public:
{
auto iv_buf_result = ByteBuffer::copy(iv_in);
// Not enough memory to figure out :shrug:
- if (!iv_buf_result.has_value()) {
+ if (iv_buf_result.is_error()) {
dbgln("GCM::encrypt: Not enough memory to allocate {} bytes for IV", iv_in.size());
return;
}
- auto iv = iv_buf_result->bytes();
+ auto iv = iv_buf_result.value().bytes();
// Increment the IV for block 0
CTR<T>::increment(iv);
@@ -98,10 +98,10 @@ public:
{
auto iv_buf_result = ByteBuffer::copy(iv_in);
// Not enough memory to figure out :shrug:
- if (!iv_buf_result.has_value())
+ if (iv_buf_result.is_error())
return VerificationConsistency::Inconsistent;
- auto iv = iv_buf_result->bytes();
+ auto iv = iv_buf_result.value().bytes();
// Increment the IV for block 0
CTR<T>::increment(iv);
diff --git a/Userland/Libraries/LibCrypto/Cipher/Mode/Mode.h b/Userland/Libraries/LibCrypto/Cipher/Mode/Mode.h
index 50c39f9f0c..0daf56eac6 100644
--- a/Userland/Libraries/LibCrypto/Cipher/Mode/Mode.h
+++ b/Userland/Libraries/LibCrypto/Cipher/Mode/Mode.h
@@ -26,7 +26,7 @@ public:
const T& cipher() const { return m_cipher; }
- Optional<ByteBuffer> create_aligned_buffer(size_t input_size) const
+ ErrorOr<ByteBuffer> create_aligned_buffer(size_t input_size) const
{
size_t remainder = (input_size + T::block_size()) % T::block_size();
if (remainder == 0)
diff --git a/Userland/Libraries/LibCrypto/NumberTheory/ModularFunctions.cpp b/Userland/Libraries/LibCrypto/NumberTheory/ModularFunctions.cpp
index 5940aa6909..4aa33b015d 100644
--- a/Userland/Libraries/LibCrypto/NumberTheory/ModularFunctions.cpp
+++ b/Userland/Libraries/LibCrypto/NumberTheory/ModularFunctions.cpp
@@ -165,7 +165,7 @@ UnsignedBigInteger random_number(const UnsignedBigInteger& min, const UnsignedBi
UnsignedBigInteger base;
auto size = range.trimmed_length() * sizeof(u32) + 2;
// "+2" is intentional (see below).
- auto buffer = ByteBuffer::create_uninitialized(size).release_value(); // FIXME: Handle possible OOM situation.
+ auto buffer = ByteBuffer::create_uninitialized(size).release_value_but_fixme_should_propagate_errors(); // FIXME: Handle possible OOM situation.
auto* buf = buffer.data();
fill_with_random(buf, size);
diff --git a/Userland/Libraries/LibCrypto/PK/RSA.cpp b/Userland/Libraries/LibCrypto/PK/RSA.cpp
index 12fed7b954..15f257fc29 100644
--- a/Userland/Libraries/LibCrypto/PK/RSA.cpp
+++ b/Userland/Libraries/LibCrypto/PK/RSA.cpp
@@ -199,7 +199,7 @@ RSA::KeyPairType RSA::parse_rsa_key(ReadonlyBytes der)
auto data = data_result.release_value();
// FIXME: This is pretty awkward, maybe just generate a zero'd out ByteBuffer from the parser instead?
auto padded_data_result = ByteBuffer::create_zeroed(data.size_in_bytes());
- if (!padded_data_result.has_value()) {
+ if (padded_data_result.is_error()) {
dbgln_if(RSA_PARSE_DEBUG, "RSA PKCS#1 key parse failed: Not enough memory");
return keypair;
}
diff --git a/Userland/Libraries/LibEDID/EDID.cpp b/Userland/Libraries/LibEDID/EDID.cpp
index 81c8ca4215..bbf8f97efe 100644
--- a/Userland/Libraries/LibEDID/EDID.cpp
+++ b/Userland/Libraries/LibEDID/EDID.cpp
@@ -361,25 +361,21 @@ ErrorOr<Parser> Parser::from_framebuffer_device(int framebuffer_fd, size_t head)
int err = errno;
if (err == EOVERFLOW) {
// We need a bigger buffer with at least bytes_size bytes
- auto edid_byte_buffer = ByteBuffer::create_zeroed(edid_info.bytes_size);
- if (!edid_byte_buffer.has_value())
- return Error::from_errno(ENOMEM);
- edid_info.bytes = edid_byte_buffer.value().data();
+ auto edid_byte_buffer = TRY(ByteBuffer::create_zeroed(edid_info.bytes_size));
+ edid_info.bytes = edid_byte_buffer.data();
if (fb_get_head_edid(framebuffer_fd, &edid_info) < 0) {
err = errno;
return Error::from_errno(err);
}
- return from_bytes(edid_byte_buffer.release_value());
+ return from_bytes(move(edid_byte_buffer));
}
return Error::from_errno(err);
}
- auto edid_byte_buffer = ByteBuffer::copy((void const*)edid_bytes, sizeof(edid_bytes));
- if (!edid_byte_buffer.has_value())
- return Error::from_errno(ENOMEM);
- return from_bytes(edid_byte_buffer.release_value());
+ auto edid_byte_buffer = TRY(ByteBuffer::copy((void const*)edid_bytes, sizeof(edid_bytes)));
+ return from_bytes(move(edid_byte_buffer));
}
ErrorOr<Parser> Parser::from_framebuffer_device(String const& framebuffer_device, size_t head)
diff --git a/Userland/Libraries/LibELF/DynamicLinker.cpp b/Userland/Libraries/LibELF/DynamicLinker.cpp
index b66b6401b5..7c5e0c225d 100644
--- a/Userland/Libraries/LibELF/DynamicLinker.cpp
+++ b/Userland/Libraries/LibELF/DynamicLinker.cpp
@@ -184,7 +184,7 @@ static void allocate_tls()
auto page_aligned_size = align_up_to(s_total_tls_size, PAGE_SIZE);
auto initial_tls_data_result = ByteBuffer::create_zeroed(page_aligned_size);
- if (!initial_tls_data_result.has_value()) {
+ if (initial_tls_data_result.is_error()) {
dbgln("Failed to allocate initial TLS data");
VERIFY_NOT_REACHED();
}
diff --git a/Userland/Libraries/LibGUI/Clipboard.cpp b/Userland/Libraries/LibGUI/Clipboard.cpp
index 3bb941ac8d..156b8a72ec 100644
--- a/Userland/Libraries/LibGUI/Clipboard.cpp
+++ b/Userland/Libraries/LibGUI/Clipboard.cpp
@@ -56,7 +56,7 @@ Clipboard::DataAndType Clipboard::fetch_data_and_type() const
if (!response.data().is_valid())
return {};
auto data = ByteBuffer::copy(response.data().data<void>(), response.data().size());
- if (!data.has_value())
+ if (data.is_error())
return {};
auto type = response.mime_type();
diff --git a/Userland/Libraries/LibGfx/BMPLoader.cpp b/Userland/Libraries/LibGfx/BMPLoader.cpp
index 1e5a0f0d6f..7008e96dc8 100644
--- a/Userland/Libraries/LibGfx/BMPLoader.cpp
+++ b/Userland/Libraries/LibGfx/BMPLoader.cpp
@@ -927,7 +927,7 @@ static bool uncompress_bmp_rle_data(BMPLoadingContext& context, ByteBuffer& buff
return false;
}
auto buffer_result = ByteBuffer::create_zeroed(buffer_size);
- if (!buffer_result.has_value()) {
+ if (buffer_result.is_error()) {
dbgln("Not enough memory for buffer allocation");
return false;
}
diff --git a/Userland/Libraries/LibGfx/BMPWriter.cpp b/Userland/Libraries/LibGfx/BMPWriter.cpp
index e52f9d07f0..0de6bf3417 100644
--- a/Userland/Libraries/LibGfx/BMPWriter.cpp
+++ b/Userland/Libraries/LibGfx/BMPWriter.cpp
@@ -46,7 +46,7 @@ static ByteBuffer write_pixel_data(const RefPtr<Bitmap> bitmap, int pixel_row_da
{
int image_size = pixel_row_data_size * bitmap->height();
auto buffer_result = ByteBuffer::create_uninitialized(image_size);
- if (!buffer_result.has_value())
+ if (buffer_result.is_error())
return {};
auto buffer = buffer_result.release_value();
@@ -100,7 +100,7 @@ ByteBuffer BMPWriter::dump(const RefPtr<Bitmap> bitmap, DibHeader dib_header)
int pixel_row_data_size = (m_bytes_per_pixel * 8 * bitmap->width() + 31) / 32 * 4;
int image_size = pixel_row_data_size * bitmap->height();
auto buffer_result = ByteBuffer::create_uninitialized(pixel_data_offset);
- if (!buffer_result.has_value())
+ if (buffer_result.is_error())
return {};
auto buffer = buffer_result.release_value();
diff --git a/Userland/Libraries/LibGfx/Bitmap.cpp b/Userland/Libraries/LibGfx/Bitmap.cpp
index 468dfe07c4..5beed09611 100644
--- a/Userland/Libraries/LibGfx/Bitmap.cpp
+++ b/Userland/Libraries/LibGfx/Bitmap.cpp
@@ -241,7 +241,7 @@ ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::try_create_from_serialized_byte_buffer(By
ByteBuffer Bitmap::serialize_to_byte_buffer() const
{
// FIXME: Somehow handle possible OOM situation here.
- auto buffer = ByteBuffer::create_uninitialized(sizeof(size_t) + 4 * sizeof(unsigned) + sizeof(BitmapFormat) + sizeof(RGBA32) * palette_size(m_format) + size_in_bytes()).release_value();
+ auto buffer = ByteBuffer::create_uninitialized(sizeof(size_t) + 4 * sizeof(unsigned) + sizeof(BitmapFormat) + sizeof(RGBA32) * palette_size(m_format) + size_in_bytes()).release_value_but_fixme_should_propagate_errors();
OutputMemoryStream stream { buffer };
auto write = [&]<typename T>(T value) {
diff --git a/Userland/Libraries/LibGfx/PNGWriter.cpp b/Userland/Libraries/LibGfx/PNGWriter.cpp
index 5ca0f43c2d..edd68be278 100644
--- a/Userland/Libraries/LibGfx/PNGWriter.cpp
+++ b/Userland/Libraries/LibGfx/PNGWriter.cpp
@@ -222,7 +222,7 @@ ByteBuffer PNGWriter::encode(Gfx::Bitmap const& bitmap)
writer.add_IDAT_chunk(bitmap);
writer.add_IEND_chunk();
// FIXME: Handle OOM failure.
- return ByteBuffer::copy(writer.m_data).release_value();
+ return ByteBuffer::copy(writer.m_data).release_value_but_fixme_should_propagate_errors();
}
}
diff --git a/Userland/Libraries/LibHTTP/Job.cpp b/Userland/Libraries/LibHTTP/Job.cpp
index 29840f9dc2..00828add3c 100644
--- a/Userland/Libraries/LibHTTP/Job.cpp
+++ b/Userland/Libraries/LibHTTP/Job.cpp
@@ -407,7 +407,7 @@ void Job::finish_up()
VERIFY(!m_has_scheduled_finish);
m_state = State::Finished;
if (!m_can_stream_response) {
- auto flattened_buffer = ByteBuffer::create_uninitialized(m_buffered_size).release_value(); // FIXME: Handle possible OOM situation.
+ auto flattened_buffer = ByteBuffer::create_uninitialized(m_buffered_size).release_value_but_fixme_should_propagate_errors(); // FIXME: Handle possible OOM situation.
u8* flat_ptr = flattened_buffer.data();
for (auto& received_buffer : m_received_buffers) {
memcpy(flat_ptr, received_buffer.data(), received_buffer.size());
diff --git a/Userland/Libraries/LibIPC/Connection.cpp b/Userland/Libraries/LibIPC/Connection.cpp
index 07b39345ff..54a805635e 100644
--- a/Userland/Libraries/LibIPC/Connection.cpp
+++ b/Userland/Libraries/LibIPC/Connection.cpp
@@ -167,14 +167,12 @@ ErrorOr<void> ConnectionBase::drain_messages_from_peer()
// Sometimes we might receive a partial message. That's okay, just stash away
// the unprocessed bytes and we'll prepend them to the next incoming message
// in the next run of this function.
- auto maybe_remaining_bytes = ByteBuffer::copy(bytes.span().slice(index));
- if (!maybe_remaining_bytes.has_value())
- return Error::from_string_literal("drain_messages_from_peer: Failed to allocate buffer"sv);
+ auto remaining_bytes = TRY(ByteBuffer::copy(bytes.span().slice(index)));
if (!m_unprocessed_bytes.is_empty()) {
shutdown();
return Error::from_string_literal("drain_messages_from_peer: Already have unprocessed bytes"sv);
}
- m_unprocessed_bytes = maybe_remaining_bytes.release_value();
+ m_unprocessed_bytes = move(remaining_bytes);
}
if (!m_unprocessed_messages.is_empty()) {
diff --git a/Userland/Libraries/LibIPC/Decoder.cpp b/Userland/Libraries/LibIPC/Decoder.cpp
index 75363fded0..0002acf7d5 100644
--- a/Userland/Libraries/LibIPC/Decoder.cpp
+++ b/Userland/Libraries/LibIPC/Decoder.cpp
@@ -8,14 +8,10 @@
#include <AK/URL.h>
#include <LibCore/AnonymousBuffer.h>
#include <LibCore/DateTime.h>
-#include <LibCore/System.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Dictionary.h>
#include <LibIPC/File.h>
-#include <errno.h>
#include <fcntl.h>
-#include <string.h>
-#include <sys/socket.h>
namespace IPC {
@@ -125,10 +121,7 @@ ErrorOr<void> Decoder::decode(ByteBuffer& value)
return {};
}
- if (auto buffer_result = ByteBuffer::create_uninitialized(length); buffer_result.has_value())
- value = buffer_result.release_value();
- else
- return Error::from_errno(ENOMEM);
+ value = TRY(ByteBuffer::create_uninitialized(length));
m_stream >> value.bytes();
return m_stream.try_handle_any_error();
diff --git a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp
index d727813183..2d03c0f743 100644
--- a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp
+++ b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp
@@ -13,7 +13,7 @@ namespace JS {
ArrayBuffer* ArrayBuffer::create(GlobalObject& global_object, size_t byte_length)
{
auto buffer = ByteBuffer::create_zeroed(byte_length);
- if (!buffer.has_value()) {
+ if (buffer.is_error()) {
global_object.vm().throw_exception<RangeError>(global_object, ErrorType::NotEnoughMemoryToAllocate, byte_length);
return nullptr;
}
@@ -62,11 +62,11 @@ ThrowCompletionOr<ArrayBuffer*> allocate_array_buffer(GlobalObject& global_objec
// 2. Let block be ? CreateByteDataBlock(byteLength).
auto block = ByteBuffer::create_zeroed(byte_length);
- if (!block.has_value())
+ if (block.is_error())
return global_object.vm().throw_completion<RangeError>(global_object, ErrorType::NotEnoughMemoryToAllocate, byte_length);
// 3. Set obj.[[ArrayBufferData]] to block.
- obj->set_buffer(move(*block));
+ obj->set_buffer(block.release_value());
// 4. Set obj.[[ArrayBufferByteLength]] to byteLength.
diff --git a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h
index ec4662509e..0cf1369fe8 100644
--- a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h
+++ b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h
@@ -133,7 +133,7 @@ static ByteBuffer numeric_to_raw_bytes(GlobalObject& global_object, Value value,
{
VERIFY(value.is_number() || value.is_bigint());
using UnderlyingBufferDataType = Conditional<IsSame<ClampedU8, T>, u8, T>;
- ByteBuffer raw_bytes = ByteBuffer::create_uninitialized(sizeof(UnderlyingBufferDataType)).release_value(); // FIXME: Handle possible OOM situation.
+ ByteBuffer raw_bytes = ByteBuffer::create_uninitialized(sizeof(UnderlyingBufferDataType)).release_value_but_fixme_should_propagate_errors(); // FIXME: Handle possible OOM situation.
auto flip_if_needed = [&]() {
if (is_little_endian)
return;
diff --git a/Userland/Libraries/LibPDF/Filter.cpp b/Userland/Libraries/LibPDF/Filter.cpp
index d521c3f210..f5c15fa326 100644
--- a/Userland/Libraries/LibPDF/Filter.cpp
+++ b/Userland/Libraries/LibPDF/Filter.cpp
@@ -45,8 +45,8 @@ Optional<ByteBuffer> Filter::decode_ascii_hex(ReadonlyBytes bytes)
// FIXME: Integrate this padding into AK/Hex?
auto output_result = ByteBuffer::create_zeroed(bytes.size() / 2 + 1);
- if (!output_result.has_value())
- return output_result;
+ if (output_result.is_error())
+ return {};
auto output = output_result.release_value();
@@ -120,7 +120,10 @@ Optional<ByteBuffer> Filter::decode_ascii85(ReadonlyBytes bytes)
buff.append(reinterpret_cast<u8*>(&number)[3 - i]);
}
- return ByteBuffer::copy(buff.span());
+ auto result = ByteBuffer::copy(buff.span());
+ if (result.is_error())
+ return {};
+ return result.release_value();
};
Optional<ByteBuffer> Filter::decode_lzw(ReadonlyBytes)
diff --git a/Userland/Libraries/LibPDF/Parser.cpp b/Userland/Libraries/LibPDF/Parser.cpp
index 20bc36fd00..24e8b03c4a 100644
--- a/Userland/Libraries/LibPDF/Parser.cpp
+++ b/Userland/Libraries/LibPDF/Parser.cpp
@@ -270,7 +270,7 @@ bool Parser::initialize_hint_tables()
auto total_size = primary_size + overflow_size;
auto buffer_result = ByteBuffer::create_uninitialized(total_size);
- if (!buffer_result.has_value())
+ if (buffer_result.is_error())
return false;
possible_merged_stream_buffer = buffer_result.release_value();
auto ok = !possible_merged_stream_buffer.try_append(primary_hint_stream->bytes()).is_error();
diff --git a/Userland/Libraries/LibProtocol/RequestClient.cpp b/Userland/Libraries/LibProtocol/RequestClient.cpp
index ac394def1f..00fda42af0 100644
--- a/Userland/Libraries/LibProtocol/RequestClient.cpp
+++ b/Userland/Libraries/LibProtocol/RequestClient.cpp
@@ -28,7 +28,7 @@ RefPtr<Request> RequestClient::start_request(String const& method, URL const& ur
header_dictionary.add(it.key, it.value);
auto body_result = ByteBuffer::copy(request_body);
- if (!body_result.has_value())
+ if (body_result.is_error())
return nullptr;
auto response = IPCProxy::start_request(method, url, header_dictionary, body_result.release_value());
diff --git a/Userland/Libraries/LibProtocol/WebSocket.cpp b/Userland/Libraries/LibProtocol/WebSocket.cpp
index c97f4027d3..88f8d7bb41 100644
--- a/Userland/Libraries/LibProtocol/WebSocket.cpp
+++ b/Userland/Libraries/LibProtocol/WebSocket.cpp
@@ -27,9 +27,7 @@ void WebSocket::send(ByteBuffer binary_or_text_message, bool is_text)
void WebSocket::send(StringView text_message)
{
- auto data_result = ByteBuffer::copy(text_message.bytes());
- VERIFY(data_result.has_value());
- send(data_result.release_value(), true);
+ send(ByteBuffer::copy(text_message.bytes()).release_value_but_fixme_should_propagate_errors(), true);
}
void WebSocket::close(u16 code, String reason)
diff --git a/Userland/Libraries/LibSQL/Heap.cpp b/Userland/Libraries/LibSQL/Heap.cpp
index 85c9a7ced5..813909df3e 100644
--- a/Userland/Libraries/LibSQL/Heap.cpp
+++ b/Userland/Libraries/LibSQL/Heap.cpp
@@ -243,7 +243,7 @@ void Heap::update_zero_block()
}
// FIXME: Handle an OOM failure here.
- auto buffer = ByteBuffer::create_zeroed(BLOCKSIZE).release_value();
+ auto buffer = ByteBuffer::create_zeroed(BLOCKSIZE).release_value_but_fixme_should_propagate_errors();
buffer.overwrite(0, FILE_ID.characters_without_null_termination(), FILE_ID.length());
buffer.overwrite(VERSION_OFFSET, &m_version, sizeof(u32));
buffer.overwrite(SCHEMAS_ROOT_OFFSET, &m_schemas_root, sizeof(u32));
diff --git a/Userland/Libraries/LibTLS/HandshakeClient.cpp b/Userland/Libraries/LibTLS/HandshakeClient.cpp
index f1f07b2bf5..85e4e8f618 100644
--- a/Userland/Libraries/LibTLS/HandshakeClient.cpp
+++ b/Userland/Libraries/LibTLS/HandshakeClient.cpp
@@ -193,7 +193,7 @@ void TLSv12::build_rsa_pre_master_secret(PacketBuilder& builder)
}
auto premaster_key_result = ByteBuffer::copy(random_bytes, bytes);
- if (!premaster_key_result.has_value()) {
+ if (premaster_key_result.is_error()) {
dbgln("RSA premaster key generation failed, not enough memory");
return;
}
@@ -245,7 +245,7 @@ void TLSv12::build_dhe_rsa_pre_master_secret(PacketBuilder& builder)
auto dh_random = Crypto::NumberTheory::random_number(0, dh_p);
auto dh_Yc = Crypto::NumberTheory::ModularPower(dh_g, dh_random, dh_p);
auto dh_Yc_bytes_result = ByteBuffer::create_uninitialized(dh_key_size);
- if (!dh_Yc_bytes_result.has_value()) {
+ if (dh_Yc_bytes_result.is_error()) {
dbgln("Failed to build DHE_RSA premaster secret: not enough memory");
return;
}
@@ -254,7 +254,7 @@ void TLSv12::build_dhe_rsa_pre_master_secret(PacketBuilder& builder)
auto premaster_key = Crypto::NumberTheory::ModularPower(dh_Ys, dh_random, dh_p);
auto premaster_key_result = ByteBuffer::create_uninitialized(dh_key_size);
- if (!premaster_key_result.has_value()) {
+ if (premaster_key_result.is_error()) {
dbgln("Failed to build DHE_RSA premaster secret: not enough memory");
return;
}
diff --git a/Userland/Libraries/LibTLS/HandshakeServer.cpp b/Userland/Libraries/LibTLS/HandshakeServer.cpp
index 7c3e37cf13..60c4e95a3f 100644
--- a/Userland/Libraries/LibTLS/HandshakeServer.cpp
+++ b/Userland/Libraries/LibTLS/HandshakeServer.cpp
@@ -241,7 +241,7 @@ ssize_t TLSv12::handle_dhe_rsa_server_key_exchange(ReadonlyBytes buffer)
auto dh_p_length = AK::convert_between_host_and_network_endian(ByteReader::load16(buffer.offset_pointer(3)));
auto dh_p = buffer.slice(5, dh_p_length);
auto p_result = ByteBuffer::copy(dh_p);
- if (!p_result.has_value()) {
+ if (p_result.is_error()) {
dbgln("dhe_rsa_server_key_exchange failed: Not enough memory");
return 0;
}
@@ -250,7 +250,7 @@ ssize_t TLSv12::handle_dhe_rsa_server_key_exchange(ReadonlyBytes buffer)
auto dh_g_length = AK::convert_between_host_and_network_endian(ByteReader::load16(buffer.offset_pointer(5 + dh_p_length)));
auto dh_g = buffer.slice(7 + dh_p_length, dh_g_length);
auto g_result = ByteBuffer::copy(dh_g);
- if (!g_result.has_value()) {
+ if (g_result.is_error()) {
dbgln("dhe_rsa_server_key_exchange failed: Not enough memory");
return 0;
}
@@ -259,7 +259,7 @@ ssize_t TLSv12::handle_dhe_rsa_server_key_exchange(ReadonlyBytes buffer)
auto dh_Ys_length = AK::convert_between_host_and_network_endian(ByteReader::load16(buffer.offset_pointer(7 + dh_p_length + dh_g_length)));
auto dh_Ys = buffer.slice(9 + dh_p_length + dh_g_length, dh_Ys_length);
auto Ys_result = ByteBuffer::copy(dh_Ys);
- if (!Ys_result.has_value()) {
+ if (Ys_result.is_error()) {
dbgln("dhe_rsa_server_key_exchange failed: Not enough memory");
return 0;
}
diff --git a/Userland/Libraries/LibTLS/Record.cpp b/Userland/Libraries/LibTLS/Record.cpp
index 1bce5a0f12..77dfc71faa 100644
--- a/Userland/Libraries/LibTLS/Record.cpp
+++ b/Userland/Libraries/LibTLS/Record.cpp
@@ -104,7 +104,7 @@ void TLSv12::update_packet(ByteBuffer& packet)
if (m_context.crypto.created == 1) {
// `buffer' will continue to be encrypted
auto buffer_result = ByteBuffer::create_uninitialized(length);
- if (!buffer_result.has_value()) {
+ if (buffer_result.is_error()) {
dbgln("LibTLS: Failed to allocate enough memory");
VERIFY_NOT_REACHED();
}
@@ -124,7 +124,7 @@ void TLSv12::update_packet(ByteBuffer& packet)
VERIFY(is_aead());
// We need enough space for a header, the data, a tag, and the IV
auto ct_buffer_result = ByteBuffer::create_uninitialized(length + header_size + iv_size + 16);
- if (!ct_buffer_result.has_value()) {
+ if (ct_buffer_result.is_error()) {
dbgln("LibTLS: Failed to allocate enough memory for the ciphertext");
VERIFY_NOT_REACHED();
}
@@ -178,7 +178,7 @@ void TLSv12::update_packet(ByteBuffer& packet)
VERIFY(!is_aead());
// We need enough space for a header, iv_length bytes of IV and whatever the packet contains
auto ct_buffer_result = ByteBuffer::create_uninitialized(length + header_size + iv_size);
- if (!ct_buffer_result.has_value()) {
+ if (ct_buffer_result.is_error()) {
dbgln("LibTLS: Failed to allocate enough memory for the ciphertext");
VERIFY_NOT_REACHED();
}
@@ -201,7 +201,7 @@ void TLSv12::update_packet(ByteBuffer& packet)
VERIFY(buffer_position == buffer.size());
auto iv_buffer_result = ByteBuffer::create_uninitialized(iv_size);
- if (!iv_buffer_result.has_value()) {
+ if (iv_buffer_result.is_error()) {
dbgln("LibTLS: Failed to allocate memory for IV");
VERIFY_NOT_REACHED();
}
@@ -293,14 +293,14 @@ ByteBuffer TLSv12::hmac_message(ReadonlyBytes buf, const Optional<ReadonlyBytes>
}
auto digest = hmac.digest();
auto mac_result = ByteBuffer::copy(digest.immutable_data(), digest.data_length());
- if (!mac_result.has_value()) {
+ if (mac_result.is_error()) {
dbgln("Failed to calculate message HMAC: Not enough memory");
return {};
}
if constexpr (TLS_DEBUG) {
dbgln("HMAC of the block for sequence number {}", sequence_number);
- print_buffer(*mac_result);
+ print_buffer(mac_result.value());
}
return mac_result.release_value();
@@ -367,7 +367,7 @@ ssize_t TLSv12::handle_message(ReadonlyBytes buffer)
auto packet_length = length - iv_length() - 16;
auto payload = plain;
auto decrypted_result = ByteBuffer::create_uninitialized(packet_length);
- if (!decrypted_result.has_value()) {
+ if (decrypted_result.is_error()) {
dbgln("Failed to allocate memory for the packet");
return_value = Error::DecryptionFailed;
return;
@@ -431,7 +431,7 @@ ssize_t TLSv12::handle_message(ReadonlyBytes buffer)
auto iv_size = iv_length();
auto decrypted_result = cbc.create_aligned_buffer(length - iv_size);
- if (!decrypted_result.has_value()) {
+ if (decrypted_result.is_error()) {
dbgln("Failed to allocate memory for the packet");
return_value = Error::DecryptionFailed;
return;
diff --git a/Userland/Libraries/LibTLS/TLSPacketBuilder.h b/Userland/Libraries/LibTLS/TLSPacketBuilder.h
index d2d3bffa32..fa43b2a40a 100644
--- a/Userland/Libraries/LibTLS/TLSPacketBuilder.h
+++ b/Userland/Libraries/LibTLS/TLSPacketBuilder.h
@@ -37,7 +37,7 @@ public:
PacketBuilder(MessageType type, Version version, size_t size_hint = 0xfdf)
{
// FIXME: Handle possible OOM situation.
- m_packet_data = ByteBuffer::create_uninitialized(size_hint + 16).release_value();
+ m_packet_data = ByteBuffer::create_uninitialized(size_hint + 16).release_value_but_fixme_should_propagate_errors();
m_current_length = 5;
m_packet_data[0] = (u8)type;
ByteReader::store(m_packet_data.offset_pointer(1), AK::convert_between_host_and_network_endian((u16)version));
diff --git a/Userland/Libraries/LibVideo/MatroskaReader.cpp b/Userland/Libraries/LibVideo/MatroskaReader.cpp
index ae51adb1fa..ce6ab8388c 100644
--- a/Userland/Libraries/LibVideo/MatroskaReader.cpp
+++ b/Userland/Libraries/LibVideo/MatroskaReader.cpp
@@ -405,7 +405,7 @@ OwnPtr<Block> MatroskaReader::parse_simple_block()
for (int i = 0; i < frame_count; i++) {
auto current_frame_size = frame_sizes.at(i);
auto frame_result = ByteBuffer::copy(m_streamer.data(), current_frame_size);
- if (!frame_result.has_value())
+ if (frame_result.is_error())
return {};
block->add_frame(frame_result.release_value());
m_streamer.drop_octets(current_frame_size);
@@ -415,14 +415,14 @@ OwnPtr<Block> MatroskaReader::parse_simple_block()
auto individual_frame_size = total_frame_content_size / frame_count;
for (int i = 0; i < frame_count; i++) {
auto frame_result = ByteBuffer::copy(m_streamer.data(), individual_frame_size);
- if (!frame_result.has_value())
+ if (frame_result.is_error())
return {};
block->add_frame(frame_result.release_value());
m_streamer.drop_octets(individual_frame_size);
}
} else {
auto frame_result = ByteBuffer::copy(m_streamer.data(), total_frame_content_size);
- if (!frame_result.has_value())
+ if (frame_result.is_error())
return {};
block->add_frame(frame_result.release_value());
m_streamer.drop_octets(total_frame_content_size);
diff --git a/Userland/Libraries/LibWeb/Bindings/IDLAbstractOperations.cpp b/Userland/Libraries/LibWeb/Bindings/IDLAbstractOperations.cpp
index cc0888e5e2..180d936d39 100644
--- a/Userland/Libraries/LibWeb/Bindings/IDLAbstractOperations.cpp
+++ b/Userland/Libraries/LibWeb/Bindings/IDLAbstractOperations.cpp
@@ -109,17 +109,17 @@ Optional<ByteBuffer> get_buffer_source_copy(JS::Object const& buffer_source)
// 8. Let bytes be a new byte sequence of length equal to length.
auto bytes = ByteBuffer::create_zeroed(length);
- if (!bytes.has_value())
+ if (bytes.is_error())
return {};
// 9. For i in the range offset to offset + length − 1, inclusive, set bytes[i − offset] to ! GetValueFromBuffer(esArrayBuffer, i, Uint8, true, Unordered).
for (u64 i = offset; i <= offset + length - 1; ++i) {
auto value = es_array_buffer->get_value<u8>(i, true, JS::ArrayBuffer::Unordered);
- (*bytes)[i - offset] = (u8)value.as_u32();
+ bytes.value()[i - offset] = (u8)value.as_u32();
}
// 10. Return bytes.
- return bytes;
+ return bytes.release_value();
}
}
diff --git a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp
index 8835b701dc..0a19c0d0ad 100644
--- a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp
+++ b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp
@@ -64,7 +64,7 @@ JS::Promise* SubtleCrypto::digest(String const& algorithm, JS::Handle<JS::Object
auto digest = hash.digest();
auto result_buffer = ByteBuffer::copy(digest.immutable_data(), hash.digest_size());
- if (!result_buffer.has_value()) {
+ if (result_buffer.is_error()) {
auto* error = wrap(wrapper()->global_object(), DOM::OperationError::create("Failed to create result buffer"));
promise->reject(error);
return promise;
diff --git a/Userland/Libraries/LibWeb/Loader/Resource.cpp b/Userland/Libraries/LibWeb/Loader/Resource.cpp
index 7da18ddc2c..64f6e72cce 100644
--- a/Userland/Libraries/LibWeb/Loader/Resource.cpp
+++ b/Userland/Libraries/LibWeb/Loader/Resource.cpp
@@ -69,7 +69,7 @@ void Resource::did_load(Badge<ResourceLoader>, ReadonlyBytes data, const HashMap
{
VERIFY(!m_loaded);
// FIXME: Handle OOM failure.
- m_encoded_data = ByteBuffer::copy(data).release_value();
+ m_encoded_data = ByteBuffer::copy(data).release_value_but_fixme_should_propagate_errors();
m_response_headers = headers;
m_status_code = move(status_code);
m_loaded = true;
diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp
index 377109270a..dd45b90027 100644
--- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp
+++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp
@@ -224,7 +224,7 @@ DOM::ExceptionOr<void> XMLHttpRequest::send(String body)
return;
auto& xhr = const_cast<XMLHttpRequest&>(*weak_this);
// FIXME: Handle OOM failure.
- auto response_data = ByteBuffer::copy(data).release_value();
+ auto response_data = ByteBuffer::copy(data).release_value_but_fixme_should_propagate_errors();
// FIXME: There's currently no difference between transmitted and length.
u64 transmitted = response_data.size();
u64 length = response_data.size();
diff --git a/Userland/Libraries/LibWebSocket/Message.h b/Userland/Libraries/LibWebSocket/Message.h
index 7192d466ee..d7b45b099f 100644
--- a/Userland/Libraries/LibWebSocket/Message.h
+++ b/Userland/Libraries/LibWebSocket/Message.h
@@ -16,7 +16,7 @@ class Message {
public:
explicit Message(String const& data)
: m_is_text(true)
- , m_data(ByteBuffer::copy(data.bytes()).release_value()) // FIXME: Handle possible OOM situation.
+ , m_data(ByteBuffer::copy(data.bytes()).release_value_but_fixme_should_propagate_errors()) // FIXME: Handle possible OOM situation.
{
}
diff --git a/Userland/Libraries/LibWebSocket/WebSocket.cpp b/Userland/Libraries/LibWebSocket/WebSocket.cpp
index 860e0c812f..6fd1873660 100644
--- a/Userland/Libraries/LibWebSocket/WebSocket.cpp
+++ b/Userland/Libraries/LibWebSocket/WebSocket.cpp
@@ -96,7 +96,7 @@ void WebSocket::close(u16 code, String const& message)
VERIFY(m_state == WebSocket::InternalState::Open);
VERIFY(m_impl);
auto message_bytes = message.bytes();
- auto close_payload = ByteBuffer::create_uninitialized(message_bytes.size() + 2).release_value(); // FIXME: Handle possible OOM situation.
+ auto close_payload = ByteBuffer::create_uninitialized(message_bytes.size() + 2).release_value_but_fixme_should_propagate_errors(); // FIXME: Handle possible OOM situation.
close_payload.overwrite(0, (u8*)&code, 2);
close_payload.overwrite(2, message_bytes.data(), message_bytes.size());
send_frame(WebSocket::OpCode::ConnectionClose, close_payload, true);
@@ -426,7 +426,7 @@ void WebSocket::read_frame()
masking_key[3] = masking_key_data[3];
}
- auto payload = ByteBuffer::create_uninitialized(payload_length).release_value(); // FIXME: Handle possible OOM situation.
+ auto payload = ByteBuffer::create_uninitialized(payload_length).release_value_but_fixme_should_propagate_errors(); // FIXME: Handle possible OOM situation.
u64 read_length = 0;
while (read_length < payload_length) {
auto payload_part = m_impl->read(payload_length - read_length);
@@ -544,7 +544,7 @@ void WebSocket::send_frame(WebSocket::OpCode op_code, ReadonlyBytes payload, boo
m_impl->send(ReadonlyBytes(masking_key, 4));
// Mask the payload
auto buffer_result = ByteBuffer::create_uninitialized(payload.size());
- if (buffer_result.has_value()) {
+ if (!buffer_result.is_error()) {
auto& masked_payload = buffer_result.value();
for (size_t i = 0; i < payload.size(); ++i) {
masked_payload[i] = payload[i] ^ (masking_key[i % 4]);
diff --git a/Userland/Services/EchoServer/Client.cpp b/Userland/Services/EchoServer/Client.cpp
index 66cec114f9..0a3ae489ba 100644
--- a/Userland/Services/EchoServer/Client.cpp
+++ b/Userland/Services/EchoServer/Client.cpp
@@ -27,10 +27,7 @@ ErrorOr<void> Client::drain_socket()
{
NonnullRefPtr<Client> protect(*this);
- auto maybe_buffer = ByteBuffer::create_uninitialized(1024);
- if (!maybe_buffer.has_value())
- return ENOMEM;
- auto buffer = maybe_buffer.release_value();
+ auto buffer = TRY(ByteBuffer::create_uninitialized(1024));
while (TRY(m_socket->can_read_without_blocking())) {
auto nread = TRY(m_socket->read(buffer));
diff --git a/Userland/Services/InspectorServer/InspectableProcess.cpp b/Userland/Services/InspectorServer/InspectableProcess.cpp
index dc5d9cb01e..cfd104703f 100644
--- a/Userland/Services/InspectorServer/InspectableProcess.cpp
+++ b/Userland/Services/InspectorServer/InspectableProcess.cpp
@@ -53,7 +53,7 @@ String InspectableProcess::wait_for_response()
return {};
}
- auto data_buffer = ByteBuffer::create_uninitialized(length).release_value();
+ auto data_buffer = ByteBuffer::create_uninitialized(length).release_value_but_fixme_should_propagate_errors();
auto remaining_data_buffer = data_buffer.bytes();
while (!remaining_data_buffer.is_empty()) {
diff --git a/Userland/Services/RequestServer/HttpCommon.h b/Userland/Services/RequestServer/HttpCommon.h
index f38e7dcee5..301ccb77f3 100644
--- a/Userland/Services/RequestServer/HttpCommon.h
+++ b/Userland/Services/RequestServer/HttpCommon.h
@@ -75,7 +75,7 @@ OwnPtr<Request> start_request(TBadgedProtocol&& protocol, ClientConnection& clie
request.set_headers(headers);
auto allocated_body_result = ByteBuffer::copy(body);
- if (!allocated_body_result.has_value())
+ if (allocated_body_result.is_error())
return {};
request.set_body(allocated_body_result.release_value());
diff --git a/Userland/Services/SpiceAgent/SpiceAgent.cpp b/Userland/Services/SpiceAgent/SpiceAgent.cpp
index 5d7f362c6a..428d74b039 100644
--- a/Userland/Services/SpiceAgent/SpiceAgent.cpp
+++ b/Userland/Services/SpiceAgent/SpiceAgent.cpp
@@ -59,7 +59,7 @@ void SpiceAgent::on_message_received()
{
ChunkHeader header {};
read_n(&header, sizeof(header));
- auto buffer = ByteBuffer::create_uninitialized(header.size).release_value(); // FIXME: Handle possible OOM situation.
+ auto buffer = ByteBuffer::create_uninitialized(header.size).release_value_but_fixme_should_propagate_errors(); // FIXME: Handle possible OOM situation.
read_n(buffer.data(), buffer.size());
auto* message = reinterpret_cast<Message*>(buffer.data());
switch (message->type) {
@@ -118,7 +118,7 @@ void SpiceAgent::on_message_received()
case (u32)MessageType::Clipboard: {
auto* clipboard_message = reinterpret_cast<Clipboard*>(message->data);
auto type = (ClipboardType)clipboard_message->type;
- auto data_buffer = ByteBuffer::create_uninitialized(message->size - sizeof(u32)).release_value(); // FIXME: Handle possible OOM situation.
+ auto data_buffer = ByteBuffer::create_uninitialized(message->size - sizeof(u32)).release_value_but_fixme_should_propagate_errors(); // FIXME: Handle possible OOM situation.
const auto total_bytes = message->size - sizeof(Clipboard);
auto bytes_copied = header.size - sizeof(Message) - sizeof(Clipboard);
@@ -209,7 +209,7 @@ SpiceAgent::Message* SpiceAgent::initialize_headers(u8* data, size_t additional_
ByteBuffer SpiceAgent::AnnounceCapabilities::make_buffer(bool request, const Vector<Capability>& capabilities)
{
size_t required_size = sizeof(ChunkHeader) + sizeof(Message) + sizeof(AnnounceCapabilities);
- auto buffer = ByteBuffer::create_uninitialized(required_size).release_value(); // FIXME: Handle possible OOM situation.
+ auto buffer = ByteBuffer::create_uninitialized(required_size).release_value_but_fixme_should_propagate_errors(); // FIXME: Handle possible OOM situation.
u8* data = buffer.data();
auto* message = initialize_headers(data, sizeof(AnnounceCapabilities), MessageType::AnnounceCapabilities);
@@ -231,7 +231,7 @@ ByteBuffer SpiceAgent::ClipboardGrab::make_buffer(const Vector<ClipboardType>& t
VERIFY(types.size() > 0);
size_t variable_data_size = sizeof(u32) * types.size();
size_t required_size = sizeof(ChunkHeader) + sizeof(Message) + variable_data_size;
- auto buffer = ByteBuffer::create_uninitialized(required_size).release_value(); // FIXME: Handle possible OOM situation.
+ auto buffer = ByteBuffer::create_uninitialized(required_size).release_value_but_fixme_should_propagate_errors(); // FIXME: Handle possible OOM situation.
u8* data = buffer.data();
auto* message = initialize_headers(data, variable_data_size, MessageType::ClipboardGrab);
@@ -249,7 +249,7 @@ ByteBuffer SpiceAgent::Clipboard::make_buffer(ClipboardType type, ReadonlyBytes
{
size_t data_size = sizeof(Clipboard) + contents.size();
size_t required_size = sizeof(ChunkHeader) + sizeof(Message) + data_size;
- auto buffer = ByteBuffer::create_uninitialized(required_size).release_value(); // FIXME: Handle possible OOM situation.
+ auto buffer = ByteBuffer::create_uninitialized(required_size).release_value_but_fixme_should_propagate_errors(); // FIXME: Handle possible OOM situation.
u8* data = buffer.data();
auto* message = initialize_headers(data, data_size, MessageType::Clipboard);
@@ -267,7 +267,7 @@ ByteBuffer SpiceAgent::ClipboardRequest::make_buffer(ClipboardType type)
{
size_t data_size = sizeof(ClipboardRequest);
size_t required_size = sizeof(ChunkHeader) + sizeof(Message) + data_size;
- auto buffer = ByteBuffer::create_uninitialized(required_size).release_value(); // FIXME: Handle possible OOM situation.
+ auto buffer = ByteBuffer::create_uninitialized(required_size).release_value_but_fixme_should_propagate_errors(); // FIXME: Handle possible OOM situation.
u8* data = buffer.data();
auto* message = initialize_headers(data, data_size, MessageType::ClipboardRequest);
diff --git a/Userland/Services/TelnetServer/Client.cpp b/Userland/Services/TelnetServer/Client.cpp
index 619652c061..aab4b4f7d0 100644
--- a/Userland/Services/TelnetServer/Client.cpp
+++ b/Userland/Services/TelnetServer/Client.cpp
@@ -73,10 +73,7 @@ ErrorOr<void> Client::drain_socket()
{
NonnullRefPtr<Client> protect(*this);
- auto maybe_buffer = ByteBuffer::create_uninitialized(1024);
- if (!maybe_buffer.has_value())
- return ENOMEM;
- auto buffer = maybe_buffer.release_value();
+ auto buffer = TRY(ByteBuffer::create_uninitialized(1024));
while (TRY(m_socket->can_read_without_blocking())) {
auto nread = TRY(m_socket->read(buffer));
@@ -196,10 +193,7 @@ ErrorOr<void> Client::send_command(Command command)
ErrorOr<void> Client::send_commands(Vector<Command> commands)
{
- auto maybe_buffer = ByteBuffer::create_uninitialized(commands.size() * 3);
- if (!maybe_buffer.has_value())
- return ENOMEM;
- auto buffer = maybe_buffer.release_value();
+ auto buffer = TRY(ByteBuffer::create_uninitialized(commands.size() * 3));
OutputMemoryStream stream { buffer };
for (auto& command : commands)
diff --git a/Userland/Services/WebServer/Client.cpp b/Userland/Services/WebServer/Client.cpp
index 86cfaf7d28..cfdfe6aab7 100644
--- a/Userland/Services/WebServer/Client.cpp
+++ b/Userland/Services/WebServer/Client.cpp
@@ -46,8 +46,8 @@ void Client::start()
StringBuilder builder;
auto maybe_buffer = ByteBuffer::create_uninitialized(m_socket->buffer_size());
- if (!maybe_buffer.has_value()) {
- warnln("Could not create buffer for client (possibly out of memory)");
+ if (maybe_buffer.is_error()) {
+ warnln("Could not create buffer for client: {}", maybe_buffer.error());
die();
return;
}
diff --git a/Userland/Shell/AST.cpp b/Userland/Shell/AST.cpp
index 4b1499028e..8d87ec3104 100644
--- a/Userland/Shell/AST.cpp
+++ b/Userland/Shell/AST.cpp
@@ -1654,7 +1654,7 @@ void Execute::for_each_entry(RefPtr<Shell> shell, Function<IterationDecision(Non
}
} else {
auto entry_result = ByteBuffer::create_uninitialized(line_end + ifs.length());
- if (!entry_result.has_value()) {
+ if (entry_result.is_error()) {
loop.quit(Break);
notifier->set_enabled(false);
return Break;
@@ -1746,7 +1746,7 @@ void Execute::for_each_entry(RefPtr<Shell> shell, Function<IterationDecision(Non
if (!stream.eof()) {
auto entry_result = ByteBuffer::create_uninitialized(stream.size());
- if (!entry_result.has_value()) {
+ if (entry_result.is_error()) {
shell->raise_error(Shell::ShellError::OutOfMemory, {}, position());
return;
}
diff --git a/Userland/Utilities/disk_benchmark.cpp b/Userland/Utilities/disk_benchmark.cpp
index d55c5073c4..551be5a491 100644
--- a/Userland/Utilities/disk_benchmark.cpp
+++ b/Userland/Utilities/disk_benchmark.cpp
@@ -96,7 +96,7 @@ int main(int argc, char** argv)
continue;
auto buffer_result = ByteBuffer::create_uninitialized(block_size);
- if (!buffer_result.has_value()) {
+ if (buffer_result.is_error()) {
warnln("Not enough memory to allocate space for block size = {}", block_size);
continue;
}
@@ -107,7 +107,7 @@ int main(int argc, char** argv)
while (timer.elapsed() < time_per_benchmark * 1000) {
out(".");
fflush(stdout);
- auto result = benchmark(filename, file_size, block_size, *buffer_result, allow_cache);
+ auto result = benchmark(filename, file_size, block_size, buffer_result.value(), allow_cache);
if (!result.has_value())
return 1;
results.append(result.release_value());
diff --git a/Userland/Utilities/ping.cpp b/Userland/Utilities/ping.cpp
index a510950cac..df24b8303a 100644
--- a/Userland/Utilities/ping.cpp
+++ b/Userland/Utilities/ping.cpp
@@ -132,7 +132,7 @@ int main(int argc, char** argv)
for (;;) {
auto ping_packet_result = ByteBuffer::create_zeroed(sizeof(struct icmphdr) + payload_size);
- if (!ping_packet_result.has_value()) {
+ if (ping_packet_result.is_error()) {
warnln("failed to allocate a large enough buffer for the ping packet");
return 1;
}
@@ -167,7 +167,7 @@ int main(int argc, char** argv)
for (;;) {
auto pong_packet_result = ByteBuffer::create_uninitialized(
sizeof(struct ip) + max_optional_header_size_in_bytes + sizeof(struct icmphdr) + payload_size);
- if (!pong_packet_result.has_value()) {
+ if (pong_packet_result.is_error()) {
warnln("failed to allocate a large enough buffer for the pong packet");
return 1;
}
diff --git a/Userland/Utilities/strace.cpp b/Userland/Utilities/strace.cpp
index c2c2d11b36..a7182f2fda 100644
--- a/Userland/Utilities/strace.cpp
+++ b/Userland/Utilities/strace.cpp
@@ -232,13 +232,9 @@ static ErrorOr<void> copy_from_process(const void* source, Bytes target)
static ErrorOr<ByteBuffer> copy_from_process(const void* source, size_t length)
{
- auto buffer = ByteBuffer::create_uninitialized(length);
- if (!buffer.has_value()) {
- // Allocation failed. Inject an error:
- return Error::from_errno(ENOMEM);
- }
- TRY(copy_from_process(source, buffer->bytes()));
- return buffer.release_value();
+ auto buffer = TRY(ByteBuffer::create_uninitialized(length));
+ TRY(copy_from_process(source, buffer.bytes()));
+ return buffer;
}
template<typename T>