[Lldb-commits] [lldb] [lldb] Fix mod-while-iteration in IRForTarget (PR #203035)

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Wed Jun 10 09:30:08 PDT 2026


https://github.com/Teemperor updated https://github.com/llvm/llvm-project/pull/203035

>From 6d868ea1fa48b4eec913b7a4fb2eec6c1c07d1de Mon Sep 17 00:00:00 2001
From: Raphael Isemann <rise at apple.com>
Date: Wed, 10 Jun 2026 16:50:35 +0100
Subject: [PATCH] [lldb] Fix mod-while-iteration in IRForTarget

We modify the IR module here while iterating over it. Use the usual
list trick to delay modification until after the loop.

This was uncovered by bccd1b9cb744e5dd96ee59baa4bf4583457feea3
---
 .../ExpressionParser/Clang/IRForTarget.cpp    | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp b/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp
index 532c1f129dab3..adfb5bd1eca94 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp
@@ -544,6 +544,9 @@ bool IRForTarget::RewriteObjCConstStrings() {
 
   ValueSymbolTable &value_symbol_table = m_module->getValueSymbolTable();
 
+  std::vector<std::pair<GlobalVariable *, GlobalVariable *>>
+      nsstring_to_cstr_list;
+
   for (StringMapEntry<llvm::Value *> &value_symbol : value_symbol_table) {
     llvm::StringRef value_name = value_symbol.first();
 
@@ -684,14 +687,16 @@ bool IRForTarget::RewriteObjCConstStrings() {
       if (!cstr_array)
         cstr_global = nullptr;
 
-      if (!RewriteObjCConstString(nsstring_global, cstr_global)) {
-        LLDB_LOG(log, "Error rewriting the constant string");
-
-        // We don't print an error message here because RewriteObjCConstString
-        // has done so for us.
+      // Queue up replacing the string as we are currently iterating
+      // over the module.
+      nsstring_to_cstr_list.emplace_back(nsstring_global, cstr_global);
+    }
+  }
 
-        return false;
-      }
+  for (auto [nsstring_global, cstr_global] : nsstring_to_cstr_list) {
+    if (!RewriteObjCConstString(nsstring_global, cstr_global)) {
+      LLDB_LOG(log, "Error rewriting the constant string");
+      return false;
     }
   }
 



More information about the lldb-commits mailing list