Skip to main content

IAR Embedded Workbench for RL78 5.20

ITR-end-cmp-bef (C++ only)

In this section:
Synopsis

An iterator is compared with end() or rend(), then dereferenced.

Enabled by default

Yes

Severity/Certainty

High/Medium

highmedium.png
Full description

An iterator is compared with end() or rend(), then dereferenced. Although it is defined behavior for iterators to have a value of end() or rend(), dereferencing them at these values is undefined, and will most likely result in illegal memory access, creating a security vulnerability in the code. This error can occur if the programmer accidentally uses the wrong comparison operator, for example == instead of !=, or if the then- and else-clauses of an if statement have accidentally changed places.

Coding standards

This check does not correspond to any coding standard rules.

Code examples

The following code example fails the check and will give a warning:

#include <vector>

int foo(){
  std::vector<int> a(5,6);
  std::vector<int>::iterator i;
  for (i = a.begin(); i != a.end(); ++i){
    ;
  }
  *i;  //here, i == a.end()
}

The following code example passes the check and will not give a warning about this issue:

#include <vector>

int foo(){
  std::vector<int> a(5,6);
  std::vector<int>::iterator i;
  *i;
  for (i = a.begin(); i != a.end(); ++i){
    *i;  //OK - i will never be a.end()
  }
}