[llvm] [RemoveDIs] Fix nullptr dereference in getFirstNonPHIIt() (PR #84595)

via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 8 17:51:40 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-ir

Author: Daniel Sanders (dsandersllvm)

<details>
<summary>Changes</summary>

getFirstNonPHI() returns nullptr for blocks that lack a non-phi (including a terminator) but getFirstNonPHIIt() may dereference its result unconditionally. Return end() instead.

This came up for us downstream while correcting our getFirstNonPHI() calls that intended to return the position after the phi's but before the debug info to getFirstNonPHIIt(). The pass in question is populating new BB's and hasn't added terminators yet.

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


1 Files Affected:

- (modified) llvm/lib/IR/BasicBlock.cpp (+2) 


``````````diff
diff --git a/llvm/lib/IR/BasicBlock.cpp b/llvm/lib/IR/BasicBlock.cpp
index 25aa3261164513..c188d2f912d16b 100644
--- a/llvm/lib/IR/BasicBlock.cpp
+++ b/llvm/lib/IR/BasicBlock.cpp
@@ -348,6 +348,8 @@ const Instruction* BasicBlock::getFirstNonPHI() const {
 
 BasicBlock::const_iterator BasicBlock::getFirstNonPHIIt() const {
   const Instruction *I = getFirstNonPHI();
+  if (!I)
+    return end();
   BasicBlock::const_iterator It = I->getIterator();
   // Set the head-inclusive bit to indicate that this iterator includes
   // any debug-info at the start of the block. This is a no-op unless the

``````````

</details>


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


More information about the llvm-commits mailing list