Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions fuzztest/internal/domains/regexp_dfa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,25 @@ std::optional<int> RegexpDFA::NextState(
std::unique_ptr<re2::Prog> RegexpDFA::CompileRegexp(absl::string_view regexp) {
// Build the RegexpDFA for only full match.
std::string full_text_regexp(regexp);
if (regexp.empty() || regexp[0] != '^')

// Check if we need to add anchors
bool needs_start_anchor = regexp.empty() || regexp[0] != '^';
bool needs_end_anchor = regexp.empty() || regexp.back() != '$';

if (needs_start_anchor || needs_end_anchor) {
full_text_regexp = "(?:" + full_text_regexp + ")";
}

if (needs_start_anchor) {
full_text_regexp = "^" + full_text_regexp;
if (full_text_regexp.back() != '$') full_text_regexp += "$";
}
if (needs_end_anchor) {
full_text_regexp += "$";
}

re2::Regexp* re =
re2::Regexp::Parse(full_text_regexp, re2::Regexp::LikePerl, nullptr);

// Is the regexp valid?
FUZZTEST_PRECONDITION(re != nullptr) << "Invalid RE2 regular expression.";
re2::Prog* prog = re->CompileToProg(0);
FUZZTEST_CHECK(prog != nullptr) << "RE2 compilation failed!";
Expand Down