1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 08:17:35 +00:00

LibCore: Propagate errors from Stream::*_entire_buffer

This commit is contained in:
Tim Schumacher 2022-12-11 19:21:36 +01:00 committed by Andreas Kling
parent 6c7c5a6786
commit 9a3e95785e
17 changed files with 43 additions and 49 deletions

View file

@ -40,7 +40,7 @@ public:
return m_stream.read(bytes);
}
virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override { return m_stream.write(bytes); }
virtual bool write_entire_buffer(ReadonlyBytes bytes) override { return m_stream.write_entire_buffer(bytes); }
virtual ErrorOr<void> write_entire_buffer(ReadonlyBytes bytes) override { return m_stream.write_entire_buffer(bytes); }
virtual bool is_eof() const override { return m_stream.is_eof() && !m_current_byte.has_value(); }
virtual bool is_open() const override { return m_stream.is_open(); }
virtual void close() override
@ -155,7 +155,7 @@ public:
return m_stream.read(bytes);
}
virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override { return m_stream.write(bytes); }
virtual bool write_entire_buffer(ReadonlyBytes bytes) override { return m_stream.write_entire_buffer(bytes); }
virtual ErrorOr<void> write_entire_buffer(ReadonlyBytes bytes) override { return m_stream.write_entire_buffer(bytes); }
virtual bool is_eof() const override { return m_stream.is_eof() && !m_current_byte.has_value(); }
virtual bool is_open() const override { return m_stream.is_open(); }
virtual void close() override

View file

@ -79,13 +79,13 @@ public:
m_offset += nwritten;
return nwritten;
}
virtual bool write_entire_buffer(ReadonlyBytes bytes) override
virtual ErrorOr<void> write_entire_buffer(ReadonlyBytes bytes) override
{
if (remaining() < bytes.size())
return false;
return Error::from_string_literal("Write of entire buffer ends past the memory area");
MUST(write(bytes));
return true;
TRY(write(bytes));
return {};
}
Bytes bytes()

View file

@ -23,14 +23,14 @@
namespace Core::Stream {
bool Stream::read_entire_buffer(Bytes buffer)
ErrorOr<void> Stream::read_entire_buffer(Bytes buffer)
{
VERIFY(buffer.size());
size_t nread = 0;
do {
if (is_eof())
return false;
return Error::from_string_literal("Reached end-of-file before filling the entire buffer");
auto result = read(buffer.slice(nread));
if (result.is_error()) {
@ -38,13 +38,13 @@ bool Stream::read_entire_buffer(Bytes buffer)
continue;
}
return false;
return result.release_error();
}
nread += result.value().size();
} while (nread < buffer.size());
return true;
return {};
}
ErrorOr<ByteBuffer> Stream::read_until_eof(size_t block_size)
@ -89,7 +89,7 @@ ErrorOr<void> Stream::discard(size_t discarded_bytes)
return {};
}
bool Stream::write_entire_buffer(ReadonlyBytes buffer)
ErrorOr<void> Stream::write_entire_buffer(ReadonlyBytes buffer)
{
VERIFY(buffer.size());
@ -101,13 +101,13 @@ bool Stream::write_entire_buffer(ReadonlyBytes buffer)
continue;
}
return false;
return result.release_error();
}
nwritten += result.value();
} while (nwritten < buffer.size());
return true;
return {};
}
ErrorOr<off_t> SeekableStream::tell() const

View file

@ -35,7 +35,7 @@ public:
virtual ErrorOr<Bytes> read(Bytes) = 0;
/// Tries to fill the entire buffer through reading. Returns whether the
/// buffer was filled without an error.
virtual bool read_entire_buffer(Bytes);
virtual ErrorOr<void> read_entire_buffer(Bytes);
/// Reads the stream until EOF, storing the contents into a ByteBuffer which
/// is returned once EOF is encountered. The block size determines the size
/// of newly allocated chunks while reading.
@ -51,7 +51,7 @@ public:
virtual ErrorOr<size_t> write(ReadonlyBytes) = 0;
/// Same as write, but does not return until either the entire buffer
/// contents are written or an error occurs.
virtual bool write_entire_buffer(ReadonlyBytes);
virtual ErrorOr<void> write_entire_buffer(ReadonlyBytes);
// This is a wrapper around `write_entire_buffer` that is compatible with
// `write_or_error`. This is required by some templated code in LibProtocol
@ -59,7 +59,7 @@ public:
// TODO: Fully port or wrap `Request::stream_into_impl` into `Core::Stream` and remove this.
bool write_or_error(ReadonlyBytes buffer)
{
return write_entire_buffer(buffer);
return !write_entire_buffer(buffer).is_error();
}
/// Returns whether the stream has reached the end of file. For sockets,

View file

@ -75,7 +75,7 @@ bool Job::can_read() const
bool Job::write(ReadonlyBytes bytes)
{
return m_socket->write_entire_buffer(bytes);
return !m_socket->write_entire_buffer(bytes).is_error();
}
void Job::flush_received_buffers()

View file

@ -220,7 +220,7 @@ void Job::on_socket_connected()
dbgln("{}", DeprecatedString::copy(raw_request));
}
bool success = m_socket->write_entire_buffer(raw_request);
bool success = !m_socket->write_entire_buffer(raw_request).is_error();
if (!success)
deferred_invoke([this] { did_fail(Core::NetworkJob::Error::TransmissionFailed); });

View file

@ -141,11 +141,11 @@ bool TLSv12::compute_master_secret_from_pre_master_secret(size_t length)
if constexpr (TLS_SSL_KEYLOG_DEBUG) {
auto file = MUST(Core::Stream::File::open("/home/anon/ssl_keylog"sv, Core::Stream::OpenMode::Append | Core::Stream::OpenMode::Write));
VERIFY(file->write_entire_buffer("CLIENT_RANDOM "sv.bytes()));
VERIFY(file->write_entire_buffer(encode_hex({ m_context.local_random, 32 }).bytes()));
VERIFY(file->write_entire_buffer(" "sv.bytes()));
VERIFY(file->write_entire_buffer(encode_hex(m_context.master_key).bytes()));
VERIFY(file->write_entire_buffer("\n"sv.bytes()));
MUST(file->write_entire_buffer("CLIENT_RANDOM "sv.bytes()));
MUST(file->write_entire_buffer(encode_hex({ m_context.local_random, 32 }).bytes()));
MUST(file->write_entire_buffer(" "sv.bytes()));
MUST(file->write_entire_buffer(encode_hex(m_context.master_key).bytes()));
MUST(file->write_entire_buffer("\n"sv.bytes()));
}
expand_key();

View file

@ -21,7 +21,7 @@ bool WebSocketImplSerenity::can_read_line()
bool WebSocketImplSerenity::send(ReadonlyBytes bytes)
{
return m_socket->write_entire_buffer(bytes);
return !m_socket->write_entire_buffer(bytes).is_error();
}
bool WebSocketImplSerenity::eof()