Fix misue of iterator pointing to erased object. Uncovered by
authorDavid Greene <greened@obbligato.org>
Fri, 29 Jun 2007 02:45:24 +0000 (02:45 +0000)
committerDavid Greene <greened@obbligato.org>
Fri, 29 Jun 2007 02:45:24 +0000 (02:45 +0000)
_GLIBCXX_DEBUG.

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

include/llvm/CodeGen/MachineBasicBlock.h
lib/CodeGen/BranchFolding.cpp
lib/CodeGen/MachineBasicBlock.cpp

index fe8a0ab30053cf044f1c330d6ef190e06540015f..df6e5a32a82bc4c5594566ec951cc11be9cf4625 100644 (file)
@@ -211,9 +211,9 @@ public:
 
   /// removeSuccessor - Remove specified successor from the successors list of
   /// this MachineBasicBlock. The Predecessors list of succ is automatically
-  /// updated.
+  /// updated.  Return the iterator to the element after the one removed.
   ///
-  void removeSuccessor(succ_iterator I);
+  succ_iterator removeSuccessor(succ_iterator I);
   
   /// isSuccessor - Return true if the specified MBB is a successor of this
   /// block.
index ad7d5fcc332485724028cafee1896f7fb204cc1c..d0dcc708d065f075a7e3b6aae338476b9e6b04ce 100644 (file)
@@ -978,17 +978,18 @@ void BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
           }
 
           // Iterate through all the predecessors, revectoring each in-turn.
-          MachineBasicBlock::pred_iterator PI = MBB->pred_begin();
+          size_t PI = 0;
           bool DidChange = false;
           bool HasBranchToSelf = false;
-          while (PI != MBB->pred_end()) {
-            if (*PI == MBB) {
+          while(PI != MBB->pred_size()) {
+            MachineBasicBlock *PMBB = *(MBB->pred_begin() + PI);
+            if (PMBB == MBB) {
               // If this block has an uncond branch to itself, leave it.
               ++PI;
               HasBranchToSelf = true;
             } else {
               DidChange = true;
-              (*PI)->ReplaceUsesOfBlockWith(MBB, CurTBB);
+              PMBB->ReplaceUsesOfBlockWith(MBB, CurTBB);
             }
           }
 
index da062b1559edc26e0bf7539413bd81dc995017fb..ba428c5bdb32938bf81314d79b755d3ce1e6a88e 100644 (file)
@@ -176,10 +176,10 @@ void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
   Successors.erase(I);
 }
 
-void MachineBasicBlock::removeSuccessor(succ_iterator I) {
+MachineBasicBlock::succ_iterator MachineBasicBlock::removeSuccessor(succ_iterator I) {
   assert(I != Successors.end() && "Not a current successor!");
   (*I)->removePredecessor(this);
-  Successors.erase(I);
+  return(Successors.erase(I));
 }
 
 void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
@@ -273,7 +273,7 @@ bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA,
       ++SI;
     } else {
       // Otherwise, this is a superfluous edge, remove it.
-      removeSuccessor(SI);
+      SI = removeSuccessor(SI);
       MadeChange = true;
     }
   }