[clang] [clang][DebugInfo] Fix iterator invalidation during EmitGlobalVariable (PR #72415)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 15 09:18:39 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang

@llvm/pr-subscribers-debuginfo

Author: Michael Buch (Michael137)

<details>
<summary>Changes</summary>

This patch fixes an issue introduced in https://github.com/llvm/llvm-project/pull/71780 where, if `EmitGlobalVariable` triggered a call to `CreateRecordStaticField` it would invalidate the `StaticDataMemberDefinitionsToEmit` iterator that
is currently in-use.

This fixes a crash reported in https://github.com/llvm/llvm-project/pull/71780#issuecomment-1812589911

---
Full diff: https://github.com/llvm/llvm-project/pull/72415.diff


1 Files Affected:

- (modified) clang/lib/CodeGen/CGDebugInfo.cpp (+7-2) 


``````````diff
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index a201cf03c22f711..d19e23e1392f4b4 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -5830,8 +5830,13 @@ void CGDebugInfo::setDwoId(uint64_t Signature) {
 }
 
 void CGDebugInfo::finalize() {
-  for (auto const *VD : StaticDataMemberDefinitionsToEmit) {
-    assert(VD->isStaticDataMember());
+  // We can't use a for-each here because `EmitGlobalVariable`
+  // may push new decls into `StaticDataMemberDefinitionsToEmit`,
+  // which would invalidate any iterator.
+  for (size_t i = 0; i < StaticDataMemberDefinitionsToEmit.size(); ++i) {
+    auto const *VD = StaticDataMemberDefinitionsToEmit[i];
+
+    assert(VD && VD->isStaticDataMember());
 
     if (DeclCache.contains(VD))
       continue;

``````````

</details>


https://github.com/llvm/llvm-project/pull/72415


More information about the cfe-commits mailing list