summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorasynts <asynts@gmail.com>2020-09-13 12:24:17 +0200
committerAndreas Kling <kling@serenityos.org>2020-09-14 20:58:12 +0200
commit96edcbc27c28af35d70d3460bd3e4a616b9f07bc (patch)
tree31fdcc5152e3880ba0e571c66e6bc34e923b8155 /AK
parent8a21c528ad9721eba2e111a468a9542bb87b53d6 (diff)
downloadserenity-96edcbc27c28af35d70d3460bd3e4a616b9f07bc.zip
AK: Lower the requirements for InputStream::eof and rename it.
Consider the following snippet: void foo(InputStream& stream) { if(!stream.eof()) { u8 byte; stream >> byte; } } There is a very subtle bug in this snippet, for some input streams eof() might return false even if no more data can be read. In this case an error flag would be set on the stream. Until now I've always ensured that this is not the case, but this made the implementation of eof() unnecessarily complicated. InputFileStream::eof had to keep a ByteBuffer around just to make this possible. That meant a ton of unnecessary copies just to get a reliable eof(). In most cases it isn't actually necessary to have a reliable eof() implementation. In most other cases a reliable eof() is avaliable anyways because in some cases like InputMemoryStream it is very easy to implement.
Diffstat (limited to 'AK')
-rw-r--r--AK/BitStream.h10
-rw-r--r--AK/Buffered.h8
-rw-r--r--AK/CircularDuplexStream.h3
-rw-r--r--AK/MemoryStream.h6
-rw-r--r--AK/Stream.h16
-rw-r--r--AK/String.h9
6 files changed, 35 insertions, 17 deletions
diff --git a/AK/BitStream.h b/AK/BitStream.h
index bd8fe28b41..fbb26d2b5c 100644
--- a/AK/BitStream.h
+++ b/AK/BitStream.h
@@ -66,7 +66,7 @@ public:
return true;
}
- bool eof() const override { return !m_next_byte.has_value() && m_stream.eof(); }
+ bool unreliable_eof() const override { return !m_next_byte.has_value() && m_stream.unreliable_eof(); }
bool discard_or_error(size_t count) override
{
@@ -86,6 +86,11 @@ public:
size_t nread = 0;
while (nread < count) {
+ if (m_stream.has_any_error()) {
+ set_fatal_error();
+ return 0;
+ }
+
if (m_next_byte.has_value()) {
const auto bit = (m_next_byte.value() >> m_bit_offset) & 1;
result |= bit << nread;
@@ -93,9 +98,6 @@ public:
if (m_bit_offset++ == 7)
m_next_byte.clear();
- } else if (m_stream.eof()) {
- set_fatal_error();
- return 0;
} else {
m_stream >> m_next_byte;
m_bit_offset = 0;
diff --git a/AK/Buffered.h b/AK/Buffered.h
index e6f72a7d98..058ecd6649 100644
--- a/AK/Buffered.h
+++ b/AK/Buffered.h
@@ -77,7 +77,7 @@ public:
return nread;
}
- virtual bool read_or_error(Bytes bytes) override
+ bool read_or_error(Bytes bytes) override
{
if (read(bytes) < bytes.size()) {
set_fatal_error();
@@ -87,7 +87,9 @@ public:
return true;
}
- virtual bool eof() const
+ bool unreliable_eof() const override { return m_buffer_remaining == 0 && m_stream.unreliable_eof(); }
+
+ bool eof() const
{
if (m_buffer_remaining > 0)
return false;
@@ -97,7 +99,7 @@ public:
return m_buffer_remaining == 0;
}
- virtual bool discard_or_error(size_t count) override
+ bool discard_or_error(size_t count) override
{
size_t ndiscarded = 0;
while (ndiscarded < count) {
diff --git a/AK/CircularDuplexStream.h b/AK/CircularDuplexStream.h
index 36e19461ef..0005d7bf4e 100644
--- a/AK/CircularDuplexStream.h
+++ b/AK/CircularDuplexStream.h
@@ -112,7 +112,8 @@ public:
return true;
}
- bool eof() const override { return m_queue.size() == 0; }
+ bool unreliable_eof() const override { return eof(); }
+ bool eof() const { return m_queue.size() == 0; }
size_t remaining_contigous_space() const
{
diff --git a/AK/MemoryStream.h b/AK/MemoryStream.h
index b58f725855..8a8bcd6196 100644
--- a/AK/MemoryStream.h
+++ b/AK/MemoryStream.h
@@ -40,7 +40,8 @@ public:
{
}
- bool eof() const override { return m_offset >= m_bytes.size(); }
+ bool unreliable_eof() const override { return eof(); }
+ bool eof() const { return m_offset >= m_bytes.size(); }
size_t read(Bytes bytes) override
{
@@ -167,7 +168,8 @@ class DuplexMemoryStream final : public DuplexStream {
public:
static constexpr size_t chunk_size = 4 * 1024;
- bool eof() const override { return m_write_offset == m_read_offset; }
+ bool unreliable_eof() const override { return eof(); }
+ bool eof() const { return m_write_offset == m_read_offset; }
bool discard_or_error(size_t count) override
{
diff --git a/AK/Stream.h b/AK/Stream.h
index d301c15052..11d286acd6 100644
--- a/AK/Stream.h
+++ b/AK/Stream.h
@@ -75,10 +75,22 @@ namespace AK {
class InputStream : public virtual AK::Detail::Stream {
public:
- // Does nothing and returns zero if there is already an error.
+ // Reads at least one byte unless none are requested or none are avaliable. Does nothing
+ // and returns zero if there is already an error.
virtual size_t read(Bytes) = 0;
+
+ // If this function returns true, then no more data can be read. If read(Bytes) previously
+ // returned zero even though bytes were requested, then the inverse is true as well.
+ virtual bool unreliable_eof() const = 0;
+
+ // Some streams additionally define a method with the signature:
+ //
+ // bool eof() const;
+ //
+ // This method has the same semantics as unreliable_eof() but returns true if and only if no
+ // more data can be read. (A failed read is not necessary.)
+
virtual bool read_or_error(Bytes) = 0;
- virtual bool eof() const = 0;
virtual bool discard_or_error(size_t count) = 0;
};
diff --git a/AK/String.h b/AK/String.h
index f4470fe98b..deed272724 100644
--- a/AK/String.h
+++ b/AK/String.h
@@ -285,16 +285,15 @@ inline InputStream& operator>>(InputStream& stream, String& string)
StringBuilder builder;
for (;;) {
- if (stream.eof()) {
- string = nullptr;
+ char next_char;
+ stream >> next_char;
+ if (stream.has_any_error()) {
stream.set_fatal_error();
+ string = nullptr;
return stream;
}
- char next_char;
- stream >> next_char;
-
if (next_char) {
builder.append(next_char);
} else {