Fix undefined behavior.

New items can be added to Ranges here, and that invalidates
an iterater that previously pointed the end of the vector.

llvm-svn: 288443
This commit is contained in:
Rui Ueyama 2016-12-02 00:38:15 +00:00
parent 42f92a7225
commit 395859bdb7
1 changed files with 9 additions and 7 deletions

View File

@ -328,19 +328,21 @@ template <class ELFT> void ICF<ELFT>::run() {
};
// Compare static contents and assign unique IDs for each static content.
auto End = Ranges.end();
foreach(Ranges.begin(), End, [&](Range &R) { segregate(&R, true); });
foreach(End, Ranges.end(), Copy);
size_t Size = Ranges.size();
foreach(Ranges.begin(), Ranges.end(),
[&](Range &R) { segregate(&R, true); });
foreach(Ranges.begin() + Size, Ranges.end(), Copy);
++Cnt;
// Split ranges by comparing relocations until convergence is obtained.
for (;;) {
auto End = Ranges.end();
foreach(Ranges.begin(), End, [&](Range &R) { segregate(&R, false); });
foreach(End, Ranges.end(), Copy);
size_t Size = Ranges.size();
foreach(Ranges.begin(), Ranges.end(),
[&](Range &R) { segregate(&R, false); });
foreach(Ranges.begin() + Size, Ranges.end(), Copy);
++Cnt;
if (End == Ranges.end())
if (Size == Ranges.size())
break;
}