diff --git a/Userland/Libraries/LibCore/Stream.cpp b/Userland/Libraries/LibCore/Stream.cpp index f6e0817731..96216e2c07 100644 --- a/Userland/Libraries/LibCore/Stream.cpp +++ b/Userland/Libraries/LibCore/Stream.cpp @@ -82,6 +82,9 @@ ErrorOr Stream::discard(size_t discarded_bytes) Array buffer; while (discarded_bytes > 0) { + if (is_eof()) + return Error::from_string_literal("Reached end-of-file before reading all discarded bytes"); + auto slice = TRY(read(buffer.span().slice(0, min(discarded_bytes, continuous_read_size)))); discarded_bytes -= slice.size(); } diff --git a/Userland/Libraries/LibCore/Stream.h b/Userland/Libraries/LibCore/Stream.h index 408ab73648..e02d1479ce 100644 --- a/Userland/Libraries/LibCore/Stream.h +++ b/Userland/Libraries/LibCore/Stream.h @@ -40,7 +40,9 @@ public: /// is returned once EOF is encountered. The block size determines the size /// of newly allocated chunks while reading. virtual ErrorOr read_until_eof(size_t block_size = 4096); - /// Discards the given number of bytes from the stream. + /// Discards the given number of bytes from the stream. As this is usually used + /// as an efficient version of `read_entire_buffer`, it returns an error + /// if reading failed or if not all bytes could be discarded. /// Unless specifically overwritten, this just uses read() to read into an /// internal stack-based buffer. virtual ErrorOr discard(size_t discarded_bytes);