From b7d8e8cf7063fdec1cce6ebd33e33804976bd978 Mon Sep 17 00:00:00 2001 From: Vladimir Marko Date: Thu, 17 Sep 2015 15:47:05 +0100 Subject: Optimizing: Do not use range-based loop when inserting elements. When we iterate over the elements of a container and we may insert new elements into that container, it's wrong to use the range-based loop. Bug: 24133462 Change-Id: Iee35fbcf88ed3bcd6155cbeba09bd256032a16be --- compiler/optimizing/nodes.cc | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'compiler/optimizing/nodes.cc') diff --git a/compiler/optimizing/nodes.cc b/compiler/optimizing/nodes.cc index 54f4071e22..b2407c520c 100644 --- a/compiler/optimizing/nodes.cc +++ b/compiler/optimizing/nodes.cc @@ -289,7 +289,10 @@ static bool CheckIfPredecessorAtIsExceptional(const HBasicBlock& block, size_t p } void HGraph::SimplifyCatchBlocks() { - for (HBasicBlock* catch_block : blocks_) { + // NOTE: We're appending new blocks inside the loop, so we need to use index because iterators + // can be invalidated. We remember the initial size to avoid iterating over the new blocks. + for (size_t block_id = 0u, end = blocks_.size(); block_id != end; ++block_id) { + HBasicBlock* catch_block = blocks_[block_id]; if (!catch_block->IsCatchBlock()) { continue; } @@ -349,7 +352,10 @@ void HGraph::SimplifyCFG() { // Simplify the CFG for future analysis, and code generation: // (1): Split critical edges. // (2): Simplify loops by having only one back edge, and one preheader. - for (HBasicBlock* block : blocks_) { + // NOTE: We're appending new blocks inside the loop, so we need to use index because iterators + // can be invalidated. We remember the initial size to avoid iterating over the new blocks. + for (size_t block_id = 0u, end = blocks_.size(); block_id != end; ++block_id) { + HBasicBlock* block = blocks_[block_id]; if (block == nullptr) continue; if (block->NumberOfNormalSuccessors() > 1) { for (size_t j = 0; j < block->GetSuccessors().size(); ++j) { -- cgit v1.2.3-59-g8ed1b