Menu ▾ ▴

False negative: invalidated iterator missed with a container reference alias

Rodri
2026-09-29
2026-09-30
  • Rodri

    Rodri - 2026-09-29

    Hi, I found a false negative regarding the rule invalidContainer.

    Cppcheck misses the use of an invalidated iterator when the vector is accessed through a reference alias.

    #include <vector>
    
    void f() {
      std::vector<int> items{1, 2, 3};
      std::vector<int> &values = items;
      auto iter = values.begin();
      values.erase(iter);
      ++iter;
    }
    

    Actual result

    Cppcheck does not report invalidContainer. Removing the reference declaration and replacing values with items produces the warning.

    Expected result

    Cppcheck should report the use of the invalidated iterator with or without the reference alias.

    Verification

    The issue was reproduced with:

    cppcheck --output-format=xmlv2 --enable=all container_reference.cpp
    

    Version: 2.21.0

     
  • CHR

    CHR - 2026-09-30
     

Log in to post a comment.