Repository navigation
regex: Constrain the lifetime of results that borrow text - #260
Merged
copybara-service[bot] merged 2 commits intoNov 11, 2026
Merged
Conversation
Find(), FindCaptures(), FindAll(), FindAllCaptures() and both Split()
overloads return objects that hold a slice of `text` on the Rust side
(Match<'h>, Captures<'h> and the iterators), but `text` had no lifetime
annotation. Passing a temporary string compiled silently and dangled:
std::optional<Match> m = re.Find(absl::StrCat(a, b));
for (absl::string_view p : re.Split(GetLine())) { ... } // pre-C++23
Mark `text` ABSL_ATTRIBUTE_LIFETIME_BOUND on all six. Clang now rejects
both (-Wdangling-gsl, through the std::string to string_view
conversion). A string_view argument, including one returned by value, a
string literal or a const std::string& is not affected, and neither is
using the result within the full-expression.
Not diagnosed, and documented on Find(): reaching the Match through
std::optional::operator->, as in re.Find(GetText())->AsStr().
Follow-up to the review of google#257.
vabr-g
reviewed
Nov 10, 2026
vabr-g
left a comment
Collaborator
There was a problem hiding this comment.
Looks good, just one minor nit.
The note on Find() put re.Find(temp)->AsStr() going undiagnosed down to std::optional::operator->. The actual reason is Match::AsStr(): Match is itself a view of `text`, so AsStr() is deliberately neither ABSL_ATTRIBUTE_LIFETIME_BOUND nor deleted for rvalues, since re.Find(text)->AsStr() must keep working. The case is therefore undiagnosed however the optional is unwrapped: ->, * or .value().
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.
Follow-up to #257, from the review discussion there.
Find(),FindCaptures(),FindAll(),FindAllCaptures()and bothSplit()overloads return objects that hold a slice oftexton the Rust side (Match<'h>,Captures<'h>and the iterators). #257 bound the ranges to theRegex, buttextstill had no annotation, so passing a temporary string compiled silently and dangled:This marks
textABSL_ATTRIBUTE_LIFETIME_BOUNDon all six. Clang reports these through-Wdangling-gsl(thestd::stringtostring_viewconversion), which is on by default.string_viewargument, including one returned by value (such as astring_viewaccessor), a string literal, aconst std::string&, and using the result within the full-expression.Find()on a temporaryRegexalso stays fine, because aMatchborrows the text, not theRegex.Find(): takingAsStr()from theMatchof a temporary in the same expression, as inre.Find(GetText())->AsStr(), however the optional is unwrapped (->,*or.value()).Matchis itself a view oftext, soMatch::AsStr()is deliberately neither lifetime-bound nor deleted for rvalues:re.Find(text)->AsStr()must keep working.No ref-qualifiers or deleted overloads change, so existing callers keep compiling unless they hit one of the cases above.
Testing
Full CMake build (Crubit f144a4f, Rust nightly-2026-11-07, clang 21.1.8):
regex,regex_exampleandregex_benchmarkbuild, andregex_exampleruns.main.cc,regex.ccandregex_benchmark.cccompile with-Werror=dangling -Werror=dangling-gsl.clang -verifytest from regex: Constrain the lifetime of Regex::AsStr(), Match::Inner() and RegexFlagWithOptions::pattern() #257, extended with 10 cases fortext(below), passes on this branch. Onmainit fails on exactly those 10.Match,Capturesor range kept fromFind(),FindCaptures()orFindAll()of a temporarystd::string, includingstd::string_view(Text())andText() + "x"; and a range-for overFindAll(),FindAllCaptures(), eitherSplit()overload, orStatusOr<Regex>->Split()of a temporary.string_views returned by value,absl::string_view(s),sv.substr(1),const char*, string literals,const std::string&andstring_viewaccessors, full-expression use, andRegex::Compile(p)->Find(s).lifetime_verify.cc