diff options
author | Rodrigo Tobar <rtobarc@gmail.com> | 2023-03-17 00:46:21 +0800 |
---|---|---|
committer | Andrew Kaster <andrewdkaster@gmail.com> | 2023-04-09 18:09:23 -0600 |
commit | 5a8373c6b9ea8bd4cf78c467da295dd5217699b2 (patch) | |
tree | ed3cda8c49098aef420a5da7c5a7b552d356314d /AK | |
parent | 371974ed4a6414002536c6784526ed5198dbcf6f (diff) | |
download | serenity-5a8373c6b9ea8bd4cf78c467da295dd5217699b2.zip |
LibCore: Fix corner case for files without newlines
When BufferedFile.can_read_line() was invoked on files with no newlines,
t incorrectly returned a false result for this single line that, even
though doesn't finish with a newline character, is still a line. Since
this method is usually used in tandem with read_line(), users would miss
reading this line (and hence all the file contents).
This commit fixes this corner case by adding another check after a
negative result from finding a newline character. This new check does
the same as the check that is done *before* looking for newlines, which
takes care of this problem, but only works for files that have at least
one newline (hence the buffer has already been filled).
A new unit test has been added that shows the use case. Without the
changes in this commit the test fails, which is a testament that this
commit really fixes the underlying issue.
Diffstat (limited to 'AK')
-rw-r--r-- | AK/BufferedStream.h | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/AK/BufferedStream.h b/AK/BufferedStream.h index c0e9512958..927c3db4d9 100644 --- a/AK/BufferedStream.h +++ b/AK/BufferedStream.h @@ -193,7 +193,10 @@ public: if (stream().is_eof()) return m_buffer.used_space() > 0; - return TRY(find_and_populate_until_any_of(Array<StringView, 1> { "\n"sv })).has_value(); + auto maybe_match = TRY(find_and_populate_until_any_of(Array { "\n"sv })); + if (maybe_match.has_value()) + return true; + return stream().is_eof() && m_buffer.used_space() > 0; } bool is_eof() const |