1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 21:17:44 +00:00

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.
This commit is contained in:
Rodrigo Tobar 2023-03-17 00:46:21 +08:00 committed by Andrew Kaster
parent 371974ed4a
commit 5a8373c6b9
2 changed files with 22 additions and 1 deletions

View file

@ -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