Skip to content
Open
Show file tree
Hide file tree
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
11 changes: 7 additions & 4 deletions re2/set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ bool RE2::Set::Compile() {
ABSL_LOG(DFATAL) << "RE2::Set::Compile() called more than once";
return false;
}
compiled_ = true;
size_ = static_cast<int>(elem_.size());

// Sort the elements by their patterns. This is good enough for now
Expand All @@ -118,16 +117,20 @@ bool RE2::Set::Compile() {
PODArray<re2::Regexp*> sub(size_);
for (int i = 0; i < size_; i++)
sub[i] = elem_[i].second;
elem_.clear();
elem_.shrink_to_fit();

Regexp::ParseFlags pf = static_cast<Regexp::ParseFlags>(
options_.ParseFlags());
re2::Regexp* re = re2::Regexp::Alternate(sub.data(), size_, pf);

prog_.reset(Prog::CompileSet(re, anchor_, options_.max_mem()));
re->Decref();
return prog_ != nullptr;

compiled_ = prog_ != nullptr;
if (compiled_) {
elem_.clear();
elem_.shrink_to_fit();
}
return compiled_;
}

bool RE2::Set::Match(absl::string_view text, std::vector<int>* v) const {
Expand Down
12 changes: 12 additions & 0 deletions re2/testing/set_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,16 @@ TEST(Set, MoveSemantics) {
ASSERT_EQ(s1.Match("abc bar2 xyz", NULL), false);
}

TEST(Set, CompileFailureDoesNotCrash) {
RE2::Options opt;
opt.set_max_mem(1);
RE2::Set s(opt, RE2::UNANCHORED);
ASSERT_EQ(s.Add("a", NULL), 0);
ASSERT_EQ(s.Size(), 1);
ASSERT_FALSE(s.Compile());
ASSERT_EQ(s.Size(), 1);
std::vector<int> v;
ASSERT_FALSE(s.Match("a", &v));
}

} // namespace re2