Skip to content

Commit

Permalink
VimEditingEngine: Prevent crash upon 'x' or 'v' on blank lines
Browse files Browse the repository at this point in the history
Previously, pressing 'x' for deletion on an otherwise empty line
insinuated a crash in TextEditor because a nonexistent code point was
accessed -- likewise for visual mode.
  • Loading branch information
arieldon authored and gunnarbeutner committed Jul 21, 2021
1 parent 7974550 commit a4fdb7f
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Userland/Libraries/LibGUI/TextDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,14 @@ String TextDocument::text_in_range(const TextRange& a_range) const
auto& line = this->line(i);
size_t selection_start_column_on_line = range.start().line() == i ? range.start().column() : 0;
size_t selection_end_column_on_line = range.end().line() == i ? range.end().column() : line.length();
builder.append(Utf32View(line.code_points() + selection_start_column_on_line, selection_end_column_on_line - selection_start_column_on_line));

if (!line.is_empty()) {
builder.append(
Utf32View(
line.code_points() + selection_start_column_on_line,
selection_end_column_on_line - selection_start_column_on_line));
}

if (i != range.end().line())
builder.append('\n');
}
Expand Down Expand Up @@ -925,12 +932,16 @@ void TextDocument::remove(const TextRange& unnormalized_range)
} else {
// Delete across a newline, merging lines.
VERIFY(range.start().line() == range.end().line() - 1);

auto& first_line = line(range.start().line());
auto& second_line = line(range.end().line());

Vector<u32> code_points;
code_points.append(first_line.code_points(), range.start().column());
code_points.append(second_line.code_points() + range.end().column(), second_line.length() - range.end().column());
if (!second_line.is_empty())
code_points.append(second_line.code_points() + range.end().column(), second_line.length() - range.end().column());
first_line.set_text(*this, move(code_points));

remove_line(range.end().line());
}

Expand Down

0 comments on commit a4fdb7f

Please sign in to comment.