[clang] [Clang][CodeGen] Segfault when compiling weird code (PR #90165)

Tim Pham via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 25 22:29:07 PDT 2024


https://github.com/timmyhoa created https://github.com/llvm/llvm-project/pull/90165

Closes #88917 

Do I really know what is going on? Nope. 
Do I know what the bug is? Enough to fix it (hopefully) :)

I'm just a beginner so sorry for any obvious mistake.  

>From 99a85014b6dd2193afcaff1cb92b92a52bb92654 Mon Sep 17 00:00:00 2001
From: timmyhoa <timmyhoa34 at gmail.com>
Date: Fri, 26 Apr 2024 01:12:20 -0400
Subject: [PATCH 1/2] fixed delete in for loop

---
 clang/lib/CodeGen/CodeGenModule.cpp | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index d085e735ecb443..cd50f06b4a976a 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -5706,6 +5706,7 @@ static void replaceUsesOfNonProtoConstant(llvm::Constant *old,
 
   llvm::Type *newRetTy = newFn->getReturnType();
   SmallVector<llvm::Value*, 4> newArgs;
+  SmallVector<llvm::CallBase* > toBeRemoved;
 
   for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end();
          ui != ue; ) {
@@ -5792,7 +5793,10 @@ static void replaceUsesOfNonProtoConstant(llvm::Constant *old,
     if (callSite->getDebugLoc())
       newCall->setDebugLoc(callSite->getDebugLoc());
 
-    callSite->eraseFromParent();
+    toBeRemoved.push_back(callSite);
+  }
+  for (llvm::CallBase *callSite : toBeRemoved) {
+	callSite->eraseFromParent();
   }
 }
 

>From 33d1a75b1c0b130b612a9dd478609e86315ddf5c Mon Sep 17 00:00:00 2001
From: timmyhoa <timmyhoa34 at gmail.com>
Date: Fri, 26 Apr 2024 01:16:08 -0400
Subject: [PATCH 2/2] formatted

---
 clang/lib/CodeGen/CodeGenModule.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index cd50f06b4a976a..7e79b6ce350beb 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -5706,7 +5706,7 @@ static void replaceUsesOfNonProtoConstant(llvm::Constant *old,
 
   llvm::Type *newRetTy = newFn->getReturnType();
   SmallVector<llvm::Value*, 4> newArgs;
-  SmallVector<llvm::CallBase* > toBeRemoved;
+  SmallVector<llvm::CallBase *> toBeRemoved;
 
   for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end();
          ui != ue; ) {
@@ -5796,7 +5796,7 @@ static void replaceUsesOfNonProtoConstant(llvm::Constant *old,
     toBeRemoved.push_back(callSite);
   }
   for (llvm::CallBase *callSite : toBeRemoved) {
-	callSite->eraseFromParent();
+    callSite->eraseFromParent();
   }
 }
 



More information about the cfe-commits mailing list