Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

On each iteration of its internal loop, for_each compares the first iterator with the second for equality, and as long as they are not equal it will continue to increment the first iterator. Of course, no matter how many times you increment the first iterator, it will never equal the second, so the loop is essentially endless. In practice, this will, at best, fall off the end of the container c and crash immediately with a memory protection fault. At worst, it will just fall off the end into uncharted memory and possibly read or change values that aren't part of the container. It's not that much different in principle from our infamous and eminently attackable friend the buffer overrun.

Wiki MarkupInvalid iterator ranges can result from comparison functions that return true for equal values \[Meyers 01\].

Non-compliant Code Example 2

...

The results are similar to non-compliant code example 1.

...

References

  • Sutter 05 Item 83: Use a checked STL implementation.
  • Meyers 01 Item 21: Always have comparison functions return false for equal values.
  • ISO/IEC 14882-2003 Section 24: Iterators Library.