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

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


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

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.

>From b74de238207e4c39e71839fde16f1ed32366c27a Mon Sep 17 00:00:00 2001
From: Daniel Sanders <daniel_l_sanders at apple.com>
Date: Thu, 7 Mar 2024 18:10:36 -0800
Subject: [PATCH] [RemoveDIs] Fix nullptr dereference in getFirstNonPHIIt()

getFirstNonPHI() returns nullptr for blocks that lack a non-phi (including
a terminator) but getFirstNonPHIIt() may dereferences 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.
---
 llvm/lib/IR/BasicBlock.cpp | 2 ++
 1 file changed, 2 insertions(+)

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



More information about the llvm-commits mailing list