mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:07:34 +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:
parent
371974ed4a
commit
5a8373c6b9
2 changed files with 22 additions and 1 deletions
|
@ -553,6 +553,24 @@ TEST_CASE(buffered_file_tell_and_seek)
|
|||
}
|
||||
}
|
||||
|
||||
constexpr auto new_newlines_message = "Hi, look, no newlines"sv;
|
||||
|
||||
TEST_CASE(buffered_file_without_newlines)
|
||||
{
|
||||
constexpr auto filename = "/tmp/file-without-newlines"sv;
|
||||
auto file_wo_newlines = Core::File::open(filename, Core::File::OpenMode::Write).release_value();
|
||||
EXPECT(!file_wo_newlines->write_until_depleted(new_newlines_message.bytes()).is_error());
|
||||
file_wo_newlines->close();
|
||||
|
||||
auto ro_file = Core::BufferedFile::create(Core::File::open(filename, Core::File::OpenMode::Read).release_value(), new_newlines_message.length() + 1).release_value();
|
||||
|
||||
auto maybe_can_read_line = ro_file->can_read_line();
|
||||
EXPECT(!maybe_can_read_line.is_error());
|
||||
EXPECT(maybe_can_read_line.release_value());
|
||||
Array<u8, new_newlines_message.length() + 1> buffer;
|
||||
EXPECT(ro_file->read_line(buffer).release_value() == new_newlines_message);
|
||||
}
|
||||
|
||||
constexpr auto buffered_sent_data = "Well hello friends!\n:^)\nThis shouldn't be present. :^("sv;
|
||||
constexpr auto first_line = "Well hello friends!"sv;
|
||||
constexpr auto second_line = ":^)"sv;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue