Menu

Possible false positive mismatchingContainerIterator

Julien
2022-02-04
2022-05-05
  • Julien

    Julien - 2022-02-04

    Hello and again thanks for the project.

    I was wondering about a warning I can reproduce with this short code sample:

    int main() {
      std::vector<int> a = {0, 1, 2, 3};
      std::vector<std::vector<int>> as = {a, a, a};
      for (auto &ais : as) {
        ais.erase(ais.begin()); // Iterator 'ais.begin()' from different container 'ais' are used together.
      }
    }
    

    Is this a false positive? I'm not sure to get why this code would be dangerous?

    Thanks!

     
  • CHR

    CHR - 2022-02-04

    Which version do you use? It seems this is fixed in head,

     
  • Julien

    Julien - 2022-02-04

    I tested it first with 2.6.2.

    But indeed, I checked now with HEAD and it looks like it works fine now. Thanks and sorry for the noise.

     
    👍
    1
  • Michael Litwak

    Michael Litwak - 2022-05-04

    I am getting a similar false-positive in version 2.7 on Windows x64:

    if (someInt >= 0)
    {
          myStdVector.erase(myStdVector.begin() + someInt);
    }
    

    Cppcheck 2.7 reports:

    Iterator 'myStdVector.begin()+someInt' from different container 'myStdVector' are used together.

     
  • CHR

    CHR - 2022-05-05

    There is no warning with current head for

    void f(std::vector<int> v, int i) {
        if (i >= 0)
        {
            v.erase(v.begin() + i);
        }
    }
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.