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

Welcome: Fix reading of welcome.txt file

The buffer returned by read_line() used to be null-terminated, however
that was changed in 129a58a, resulting in some line strings containing
garbage data. Explicitly telling the String constructor the buffer's
size fixes that.

Fixes #4397.
This commit is contained in:
Linus Groh 2020-12-12 22:15:41 +00:00 committed by Andreas Kling
parent 94c56d16b3
commit f7bd6e2b34

View file

@ -55,28 +55,17 @@ struct ContentPage {
static Optional<Vector<ContentPage>> parse_welcome_file(const String& path) static Optional<Vector<ContentPage>> parse_welcome_file(const String& path)
{ {
const auto error = Optional<Vector<ContentPage>>();
auto file = Core::File::construct(path); auto file = Core::File::construct(path);
if (!file->open(Core::IODevice::ReadOnly)) if (!file->open(Core::IODevice::ReadOnly))
return error; return {};
Vector<ContentPage> pages; Vector<ContentPage> pages;
StringBuilder current_output_line; StringBuilder current_output_line;
bool started = false; bool started = false;
ContentPage current; ContentPage current;
while (true) { while (file->can_read_line()) {
auto buffer = file->read_line(4096); auto buffer = file->read_line(4096);
if (buffer.is_null()) { auto line = String((const char*)buffer.data(), buffer.size());
if (file->error()) {
file->close();
return error;
}
break;
}
auto line = String((char*)buffer.data());
if (line.length() > 1) if (line.length() > 1)
line = line.substring(0, line.length() - 1); // remove newline line = line.substring(0, line.length() - 1); // remove newline
switch (line[0]) { switch (line[0]) {
@ -122,7 +111,6 @@ static Optional<Vector<ContentPage>> parse_welcome_file(const String& path)
pages.append(current); pages.append(current);
} }
file->close();
return pages; return pages;
} }