diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-01-20 17:47:39 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-01-24 22:36:09 +0100 |
commit | 45cf40653a03dab11c0739783446ff696a9a5b0a (patch) | |
tree | 1611c797d1a43a106cf7220fcbdbba907f19d037 /Userland/Services | |
parent | 140f1d9e55bfacb6f784bee591a6938714ed95b3 (diff) | |
download | serenity-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. :^)
Diffstat (limited to 'Userland/Services')
-rw-r--r-- | Userland/Services/EchoServer/Client.cpp | 5 | ||||
-rw-r--r-- | Userland/Services/InspectorServer/InspectableProcess.cpp | 2 | ||||
-rw-r--r-- | Userland/Services/RequestServer/HttpCommon.h | 2 | ||||
-rw-r--r-- | Userland/Services/SpiceAgent/SpiceAgent.cpp | 12 | ||||
-rw-r--r-- | Userland/Services/TelnetServer/Client.cpp | 10 | ||||
-rw-r--r-- | Userland/Services/WebServer/Client.cpp | 4 |
6 files changed, 13 insertions, 22 deletions
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; } |