[clang] [LifetimeSafety] Detect iterator invalidation through container aliases (PR #195231)
Zeyi Xu via cfe-commits
cfe-commits at lists.llvm.org
Fri May 1 22:51:27 PDT 2026
================
@@ -254,6 +253,46 @@ void IteratorUsedAfterPushBack(std::vector<int> v) {
}
} // namespace SimpleInvalidIterators
+namespace InvalidatingThroughContainerAliases {
+void IteratorInvalidatedThroughLocalReferenceAlias() {
+ std::vector<int> vv;
+ std::vector<int> &v = vv;
+ auto it = vv.begin(); // expected-warning {{object whose reference is captured is later invalidated}}
+ v.push_back(42); // expected-note {{invalidated here}}
+ (void)it; // expected-note {{later used here}}
+}
+
+void IteratorInvalidatedThroughPointerParameter(std::vector<int> *v) { // expected-warning {{parameter is later invalidated}}
+ auto it = v->begin();
+ v->push_back(42); // expected-note {{invalidated here}}
+ (void)it; // expected-note {{later used here}}
+}
+} // namespace InvalidatingThroughContainerAliases
+
+namespace ContainerObjectAliases {
+// FIXME: Distinguish owner-borrow from content-borrow.
----------------
zeyi2 wrote:
Thanks for the feedback :)
In earlier discussion in https://github.com/llvm/llvm-project/issues/193044#issuecomment-4333628748
> Invalidations already has quite a few false-positives atm and all of them are because of this limitation. So additional FP should be fine for now. We can fix them in precise path modelling in a separate PR.
Given that these diagnostics are not broadly recommended yet, I was assuming this tradeoff is acceptable for this PR, so I kept these as FIXME tests.
https://github.com/llvm/llvm-project/pull/195231
More information about the cfe-commits
mailing list