[ARM] Prevent use of a value pointed by end() iterator when placing a jump table
authorPetr Pavlu <petr.pavlu@arm.com>
Mon, 16 Nov 2015 16:41:13 +0000 (16:41 +0000)
committerPetr Pavlu <petr.pavlu@arm.com>
Mon, 16 Nov 2015 16:41:13 +0000 (16:41 +0000)
Function ARMConstantIslands::doInitialJumpTablePlacement() iterates over all
basic blocks in a machine function. It calls `MI = MBB.getLastNonDebugInstr()`
to get the last instruction in each block and then uses MI->getOpcode() to
decide what to do. If getLastNonDebugInstr() returns MBB.end() (for example,
when the block does not contain any instructions) then calling getOpcode() on
this value is incorrect. Avoid this problem by checking the result of
getLastNonDebugInstr().

Differential Revision: http://reviews.llvm.org/D14694

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@253222 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/ARM/ARMConstantIslandPass.cpp

index 416c5fd5cd7bd63e61c4cad2fd969327b1bdf3c4..0bf2d374df6a455b58634db6d769f4f4777780f5 100644 (file)
@@ -589,6 +589,8 @@ void ARMConstantIslands::doInitialJumpTablePlacement(
   MachineBasicBlock *LastCorrectlyNumberedBB = nullptr;
   for (MachineBasicBlock &MBB : *MF) {
     auto MI = MBB.getLastNonDebugInstr();
   MachineBasicBlock *LastCorrectlyNumberedBB = nullptr;
   for (MachineBasicBlock &MBB : *MF) {
     auto MI = MBB.getLastNonDebugInstr();
+    if (MI == MBB.end())
+      continue;
 
     unsigned JTOpcode;
     switch (MI->getOpcode()) {
 
     unsigned JTOpcode;
     switch (MI->getOpcode()) {