[trivialre] fix past-end-of-string access in MatchSubstring

Thanks goes to MSVC's string_view asserts that check this.
This commit is contained in:
Aliaksei Kandratsenka 2024-09-16 19:03:58 -04:00
parent 38abfd9038
commit 5273567470
2 changed files with 4 additions and 2 deletions

View File

@ -58,14 +58,14 @@ inline bool MatchSubstring(const Matcher& m, std::string_view str) {
size_t sz = str.size(); size_t sz = str.size();
CB succeed = [](std::string_view str, bool line_start) { return true; }; CB succeed = [](std::string_view str, bool line_start) { return true; };
bool line_start = true; bool line_start = true;
for (size_t i = 0; i <= sz; i++) { for (size_t i = 0; i < sz; i++) {
if (m(str, line_start, succeed)) { if (m(str, line_start, succeed)) {
return true; return true;
} }
line_start = (str[0] == '\n'); line_start = (str[0] == '\n');
str.remove_prefix(1); str.remove_prefix(1);
} }
return false; return m("", line_start, succeed);
} }
Matcher CompileREOrDie(std::string_view str); Matcher CompileREOrDie(std::string_view str);

View File

@ -154,6 +154,8 @@ TEST(TrivialRETest, Runnings) {
{"^a", "+a", "-ba", "+b\na"}, {"^a", "+a", "-ba", "+b\na"},
{"a$", "+a\nb", "+ba", "+b\na"}, {"a$", "+a\nb", "+ba", "+b\na"},
{"a$\\nb", "+a\nb"}, {"a$\\nb", "+a\nb"},
{"$", "+", "+aaa"},
{"^$", "+", "-aaa", "+aaa\n"},
}; };
for (const auto& vec : cases2) { for (const auto& vec : cases2) {