[clang] a1ab1f7 - [clang][Sema] Fix iterator invalidation in isLayoutCompatibleUnion (#219112)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 27 08:25:03 PDT 2026
Author: Kazu Hirata
Date: 2026-08-27T08:24:57-07:00
New Revision: a1ab1f7fa23288361d3d081585a17de09c4776b3
URL: https://github.com/llvm/llvm-project/commit/a1ab1f7fa23288361d3d081585a17de09c4776b3
DIFF: https://github.com/llvm/llvm-project/commit/a1ab1f7fa23288361d3d081585a17de09c4776b3.diff
LOG: [clang][Sema] Fix iterator invalidation in isLayoutCompatibleUnion (#219112)
This patch fixes an iterator invalidation bug in
isLayoutCompatibleUnion.
Without this patch, if we delete a matching field, we end up
evaluating I == E even though the iterators are invalidated. Deleting
a match after the loop fixes the problem.
This bug was discovered with tightened epoch checks in
SmallPtrSetIterator.
Assisted-by: Antigravity
Added:
Modified:
clang/lib/Sema/SemaChecking.cpp
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 7f3ccea82e8af..5c831e6cdebce 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -16592,19 +16592,13 @@ static bool isLayoutCompatibleUnion(const ASTContext &C, const RecordDecl *RD1,
RD2->fields());
for (auto *Field1 : RD1->fields()) {
- auto I = UnmatchedFields.begin();
- auto E = UnmatchedFields.end();
-
- for ( ; I != E; ++I) {
- if (isLayoutCompatible(C, Field1, *I, /*IsUnionMember=*/true)) {
- bool Result = UnmatchedFields.erase(*I);
- (void) Result;
- assert(Result);
- break;
- }
- }
- if (I == E)
+ auto It = llvm::find_if(UnmatchedFields, [&](const FieldDecl *Field2) {
+ return isLayoutCompatible(C, Field1, Field2, /*IsUnionMember=*/true);
+ });
+ if (It == UnmatchedFields.end())
return false;
+ [[maybe_unused]] bool Result = UnmatchedFields.erase(*It);
+ assert(Result);
}
return UnmatchedFields.empty();
More information about the cfe-commits
mailing list