Skip to content

Commit c4acf6f

Browse files
committed
Fix crash in cloneBlocks when terminator is missing
1 parent 63fccf4 commit c4acf6f

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

gen/ms-cxx-helper.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,21 @@ void cloneBlocks(const std::vector<llvm::BasicBlock *> &srcblocks,
8383
llvm::BasicBlock *continueWith, llvm::BasicBlock *unwindTo,
8484
llvm::Value *funclet) {
8585
llvm::ValueToValueMapTy VMap;
86+
8687
// map the terminal branch to the new target
87-
if (continueWith)
88-
if (auto term = srcblocks.back()->getTerminator())
89-
if (auto succ = term->getSuccessor(0))
90-
VMap[succ] = continueWith;
88+
if (continueWith) {
89+
if (auto term = srcblocks.back()->getTerminator()) {
90+
// FIX: Ensure it is actually a terminator before checking successors
91+
if (term->isTerminator() && term->getNumSuccessors() > 0) {
92+
VMap[term->getSuccessor(0)] = continueWith;
93+
}
94+
}
95+
}
9196

9297
for (auto bb : srcblocks) {
9398
llvm::Function *F = bb->getParent();
94-
9599
auto nbb = llvm::BasicBlock::Create(bb->getContext(), bb->getName());
96-
// Loop over all instructions, and copy them over.
100+
97101
for (auto &II : *bb) {
98102
llvm::Instruction *Inst = &II;
99103
llvm::Instruction *newInst = nullptr;
@@ -114,8 +118,8 @@ void cloneBlocks(const std::vector<llvm::BasicBlock *> &srcblocks,
114118
newInst = Inst->clone();
115119

116120
newInst->insertInto(nbb, nbb->end());
117-
118-
VMap[Inst] = newInst; // Add instruction map to value.
121+
VMap[Inst] = newInst;
122+
119123
if (unwindTo)
120124
if (auto dest = getUnwindDest(Inst))
121125
VMap[dest] = unwindTo;

0 commit comments

Comments
 (0)