summaryrefslogtreecommitdiff
path: root/Userland/Utilities/grep.cpp
diff options
context:
space:
mode:
authorMacDue <macdue@dueutil.tech>2022-07-27 22:35:13 +0100
committerBrian Gianforcaro <b.gianfo@gmail.com>2022-07-27 22:51:54 +0000
commit281e46e8b3820da483ee0aa980267ef1ce4b2d54 (patch)
treed7a9ca7429eea8e7791822123ce47afee92c557e /Userland/Utilities/grep.cpp
parent97cc33ca47c21f21c13d0b386511f589b25b7998 (diff)
downloadserenity-281e46e8b3820da483ee0aa980267ef1ce4b2d54.zip
grep: Fix out of bounds StringView indexing
This is another case of out of bounds indexing exposed by 13406b8.
Diffstat (limited to 'Userland/Utilities/grep.cpp')
-rw-r--r--Userland/Utilities/grep.cpp6
1 files changed, 4 insertions, 2 deletions
diff --git a/Userland/Utilities/grep.cpp b/Userland/Utilities/grep.cpp
index 67c7cf724a..0fb9fe8188 100644
--- a/Userland/Utilities/grep.cpp
+++ b/Userland/Utilities/grep.cpp
@@ -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;