LibCore: Allow zero-sized spans in Stream::*_entire_buffer

There is no particular reason why we shouldn't allow zero-sized reads or
writes here, and this actually might cause issues with our common
stream-to-stream copy pattern if we end up at an unfortunate offset
where the next read would be zero-sized and trigger EOF only after that.
This commit is contained in:
Tim Schumacher 2023-01-08 21:57:16 +01:00 committed by Andrew Kaster
parent d23f0a7405
commit 100112134d
Notes: sideshowbarker 2024-07-17 04:10:16 +09:00

View file

@ -24,10 +24,8 @@ namespace Core::Stream {
ErrorOr<void> Stream::read_entire_buffer(Bytes buffer) ErrorOr<void> Stream::read_entire_buffer(Bytes buffer)
{ {
VERIFY(buffer.size());
size_t nread = 0; size_t nread = 0;
do { while (nread < buffer.size()) {
if (is_eof()) if (is_eof())
return Error::from_string_literal("Reached end-of-file before filling the entire buffer"); return Error::from_string_literal("Reached end-of-file before filling the entire buffer");
@ -41,7 +39,7 @@ ErrorOr<void> Stream::read_entire_buffer(Bytes buffer)
} }
nread += result.value().size(); nread += result.value().size();
} while (nread < buffer.size()); }
return {}; return {};
} }
@ -93,10 +91,8 @@ ErrorOr<void> Stream::discard(size_t discarded_bytes)
ErrorOr<void> Stream::write_entire_buffer(ReadonlyBytes buffer) ErrorOr<void> Stream::write_entire_buffer(ReadonlyBytes buffer)
{ {
VERIFY(buffer.size());
size_t nwritten = 0; size_t nwritten = 0;
do { while (nwritten < buffer.size()) {
auto result = write(buffer.slice(nwritten)); auto result = write(buffer.slice(nwritten));
if (result.is_error()) { if (result.is_error()) {
if (result.error().is_errno() && result.error().code() == EINTR) { if (result.error().is_errno() && result.error().code() == EINTR) {
@ -107,7 +103,7 @@ ErrorOr<void> Stream::write_entire_buffer(ReadonlyBytes buffer)
} }
nwritten += result.value(); nwritten += result.value();
} while (nwritten < buffer.size()); }
return {}; return {};
} }