Skip to content

Commit 3fccf24

Browse files
timschumiawesomekling
authored andcommitted
LibCore: Make not discarding all requested bytes from a stream an error
1 parent 9a3e957 commit 3fccf24

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

Userland/Libraries/LibCore/Stream.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ ErrorOr<void> Stream::discard(size_t discarded_bytes)
8282
Array<u8, continuous_read_size> buffer;
8383

8484
while (discarded_bytes > 0) {
85+
if (is_eof())
86+
return Error::from_string_literal("Reached end-of-file before reading all discarded bytes");
87+
8588
auto slice = TRY(read(buffer.span().slice(0, min(discarded_bytes, continuous_read_size))));
8689
discarded_bytes -= slice.size();
8790
}

Userland/Libraries/LibCore/Stream.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ class Stream {
4040
/// is returned once EOF is encountered. The block size determines the size
4141
/// of newly allocated chunks while reading.
4242
virtual ErrorOr<ByteBuffer> read_until_eof(size_t block_size = 4096);
43-
/// Discards the given number of bytes from the stream.
43+
/// Discards the given number of bytes from the stream. As this is usually used
44+
/// as an efficient version of `read_entire_buffer`, it returns an error
45+
/// if reading failed or if not all bytes could be discarded.
4446
/// Unless specifically overwritten, this just uses read() to read into an
4547
/// internal stack-based buffer.
4648
virtual ErrorOr<void> discard(size_t discarded_bytes);

0 commit comments

Comments
 (0)