[clang] [LifetimeSafety] Detect iterator invalidation through container aliases (PR #195231)
Gábor Horváth via cfe-commits
cfe-commits at lists.llvm.org
Fri May 1 07:14:15 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.
----------------
Xazax-hun wrote:
Are we concerned about false positives of this shape? This might be a non-trivial amount of false positives. That being said, I think we do not really recommend people to use the invalidation related diagnostics just yet.
https://github.com/llvm/llvm-project/pull/195231
More information about the cfe-commits
mailing list