Skip to content

Commit 1001121

Browse files
timschumiADKaster
authored andcommitted
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.
1 parent d23f0a7 commit 1001121

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

Userland/Libraries/LibCore/Stream.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ namespace Core::Stream {
2424

2525
ErrorOr<void> Stream::read_entire_buffer(Bytes buffer)
2626
{
27-
VERIFY(buffer.size());
28-
2927
size_t nread = 0;
30-
do {
28+
while (nread < buffer.size()) {
3129
if (is_eof())
3230
return Error::from_string_literal("Reached end-of-file before filling the entire buffer");
3331

@@ -41,7 +39,7 @@ ErrorOr<void> Stream::read_entire_buffer(Bytes buffer)
4139
}
4240

4341
nread += result.value().size();
44-
} while (nread < buffer.size());
42+
}
4543

4644
return {};
4745
}
@@ -93,10 +91,8 @@ ErrorOr<void> Stream::discard(size_t discarded_bytes)
9391

9492
ErrorOr<void> Stream::write_entire_buffer(ReadonlyBytes buffer)
9593
{
96-
VERIFY(buffer.size());
97-
9894
size_t nwritten = 0;
99-
do {
95+
while (nwritten < buffer.size()) {
10096
auto result = write(buffer.slice(nwritten));
10197
if (result.is_error()) {
10298
if (result.error().is_errno() && result.error().code() == EINTR) {
@@ -107,7 +103,7 @@ ErrorOr<void> Stream::write_entire_buffer(ReadonlyBytes buffer)
107103
}
108104

109105
nwritten += result.value();
110-
} while (nwritten < buffer.size());
106+
}
111107

112108
return {};
113109
}

0 commit comments

Comments
 (0)