Fix self-move-assignment UB in RE2::Set and FilteredRE2, add missing compiled_ guards#637
Open
XananasX7 wants to merge 4 commits into
Open
Fix self-move-assignment UB in RE2::Set and FilteredRE2, add missing compiled_ guards#637XananasX7 wants to merge 4 commits into
XananasX7 wants to merge 4 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes two classes of undefined behavior in
RE2::SetandFilteredRE2:Fix 1: Self-move-assignment undefined behavior (UB) in
RE2::SetandFilteredRE2Files:
re2/set.cc,re2/filtered_re2.ccThe move assignment operators in both classes use a destroy-then-placement-new pattern:
This is undefined behavior when
this == &other(self-move-assignment). The destructor runs first, callingDecref()on internalRegexp*pointers and freeing resources. The subsequent move constructor then reads from the already-destroyed object — a use-after-free leading to potential double-free and heap corruption.Self-move-assignment can occur through aliased references, for example:
std::vector<RE2::Set> v = ...; v[i] = std::move(v[j]); // undefined behavior if i == jFix: Add a self-assignment guard (
if (this != &other)) before the destroy-and-reconstruct sequence, matching the standard idiomatic fix for this pattern.Fix 2: Missing
compiled_guard inFilteredRE2::AllMatches()andFilteredRE2::AllPotentials()File:
re2/filtered_re2.ccFilteredRE2::FirstMatch()correctly checks thecompiled_flag (line 100) and returns early with aDFATALlog ifCompile()was not called. However,AllMatches()andAllPotentials()lack this guard entirely.Calling either function before
Compile()results in undefined behavior because theprefilter_tree_has not been built — the same class of crash previously fixed forFilter.Match()in the Python bindings (issue #484).Fix: Add identical
compiled_guards toAllMatches()andAllPotentials(), consistent withFirstMatch().Both fixes are minimal, targeted, and follow the existing style of the codebase.