grep: Fix out of bounds StringView indexing

This is another case of out of bounds indexing exposed by 13406b8.
This commit is contained in:
MacDue 2022-07-27 22:35:13 +01:00 committed by Brian Gianforcaro
parent 97cc33ca47
commit 281e46e8b3
Notes: sideshowbarker 2024-07-17 08:31:30 +09:00

View File

@ -175,12 +175,14 @@ ErrorOr<int> serenity_main(Main::Arguments args)
out(colored_output ? "\x1B[35m{}:\x1B[0m"sv : "{}:"sv, line_number);
for (auto& match : result.matches) {
auto pre_match_length = match.global_offset - last_printed_char_pos;
out(colored_output ? "{}\x1B[32m{}\x1B[0m"sv : "{}{}"sv,
StringView(&str[last_printed_char_pos], match.global_offset - last_printed_char_pos),
pre_match_length > 0 ? StringView(&str[last_printed_char_pos], pre_match_length) : ""sv,
match.view.to_string());
last_printed_char_pos = match.global_offset + match.view.length();
}
outln("{}", StringView(&str[last_printed_char_pos], str.length() - last_printed_char_pos));
auto remaining_length = str.length() - last_printed_char_pos;
outln("{}", remaining_length > 0 ? StringView(&str[last_printed_char_pos], remaining_length) : ""sv);
}
return true;