cmAlgorithms: Add early return in cmRemoveIndices.

Avoid derefencing the iterator and segfaulting if the range
is empty.
This commit is contained in:
Stephen Kelly 2015-03-05 23:14:27 +01:00
parent 1a8e4c8692
commit 3ff95f3b0b
1 changed files with 5 additions and 1 deletions

View File

@ -237,6 +237,11 @@ typename Range::const_iterator cmRemoveIndices(Range& r, InputRange const& rem)
{ {
typename InputRange::const_iterator remIt = rem.begin(); typename InputRange::const_iterator remIt = rem.begin();
typename InputRange::const_iterator remEnd = rem.end(); typename InputRange::const_iterator remEnd = rem.end();
const typename Range::iterator rangeEnd = r.end();
if (remIt == remEnd)
{
return rangeEnd;
}
typename Range::iterator writer = r.begin(); typename Range::iterator writer = r.begin();
std::advance(writer, *remIt); std::advance(writer, *remIt);
@ -244,7 +249,6 @@ typename Range::const_iterator cmRemoveIndices(Range& r, InputRange const& rem)
typename InputRange::value_type prevRem = *remIt; typename InputRange::value_type prevRem = *remIt;
++remIt; ++remIt;
size_t count = 1; size_t count = 1;
const typename Range::iterator rangeEnd = r.end();
for ( ; writer != rangeEnd && remIt != remEnd; ++count, ++remIt) for ( ; writer != rangeEnd && remIt != remEnd; ++count, ++remIt)
{ {
std::advance(pivot, *remIt - prevRem); std::advance(pivot, *remIt - prevRem);