Skip to content

Commit

Permalink
Welcome: Fix reading of welcome.txt file
Browse files Browse the repository at this point in the history
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 SerenityOS#4397.
  • Loading branch information
linusg authored and awesomekling committed Dec 13, 2020
1 parent 94c56d1 commit f7bd6e2
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions Applications/Welcome/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,17 @@ struct ContentPage {

static Optional<Vector<ContentPage>> parse_welcome_file(const String& path)
{
const auto error = Optional<Vector<ContentPage>>();
auto file = Core::File::construct(path);

if (!file->open(Core::IODevice::ReadOnly))
return error;
return {};

Vector<ContentPage> pages;
StringBuilder current_output_line;
bool started = false;
ContentPage current;
while (true) {
while (file->can_read_line()) {
auto buffer = file->read_line(4096);
if (buffer.is_null()) {
if (file->error()) {
file->close();
return error;
}

break;
}

auto line = String((char*)buffer.data());
auto line = String((const char*)buffer.data(), buffer.size());
if (line.length() > 1)
line = line.substring(0, line.length() - 1); // remove newline
switch (line[0]) {
Expand Down Expand Up @@ -122,7 +111,6 @@ static Optional<Vector<ContentPage>> parse_welcome_file(const String& path)
pages.append(current);
}

file->close();
return pages;
}

Expand Down

0 comments on commit f7bd6e2

Please sign in to comment.