Menu

3 false positives after upgrade to 2.3

2020-12-14
2020-12-18
  • Dan Workman

    Dan Workman - 2020-12-14

    Hi all, our team recently upgraded from 1.9 to 2.3 and we are now getting quite a few false positives for our project.

    We have 8 different instances of the following issue in our code-base, i.e. populating maps via a function call and a reference:

    #include <iostream>
    #include <map>
    #include <vector>
    
    struct Foo { int bar; };
    
    void AddToMap(const std::vector<Foo>& foos, std::map<int, Foo>& fooMap)
    {
      fooMap[0] = foos.front();
    }
    
    int main()
    {
      std::map<int, Foo> fooMap;
      AddToMap({{0}, {1}, {2}}, fooMap);
    
      for (auto const [key, value] : fooMap)
        std::cout << value.bar;
    
      std::cout << fooMap.begin()->second.bar;
    }
    

    this will trigger these two warning:

    cppcheckWarnings.cpp:23:34: style: Iterating over container 'fooMap' that is always empty. [knownEmptyContainer]
      for (auto const [key, value] : fooMap)
                                     ^
    cppcheckWarnings.cpp:26:28: error: Out of bounds access in expression 'fooMap.begin()' because 'fooMap' is empty. [containerOutOfBounds]
      std::cout << fooMap.begin()->second.bar;
                               ^
    

    We also have another that requires --enable=all:

    #include <iostream>
    #include <vector>
    
    struct RasterArray
    {
      std::vector<int> m_data;
      int m_sizeX;
      int m_sizeY;
    
      RasterArray(const int sizeX, const int sizeY)
      {
        m_sizeX = sizeX;
        m_sizeY = sizeY;
    
        m_data.resize(m_sizeX * m_sizeY);
      }
    
      int &operator()(const int x, const int y)
      {
        return m_data[x*m_sizeY+y];
      }
    };
    
    using RasterArrays = std::vector<RasterArray>;
    
    void RasterArrays_SetPxl(RasterArrays& rasterArrays, const int arri, const int ix, const int iy, const int value)
    {
      rasterArrays[arri](ix, iy) = value;
    }
    
    int main()
    {
      RasterArrays rasterArrays{{10, 10}};
    
      RasterArrays_SetPxl(rasterArrays, 0, 0, 0, 42);
    
      std::cout << rasterArrays[0](0, 0);
    }
    

    which triggers the following:

    cppcheckWarnings.cpp:29:40: style: Parameter 'rasterArrays' can be declared with const [constParameter]
    void RasterArrays_SetPxl(RasterArrays& rasterArrays, const int arri, const int ix, const int iy, const int value)
    

    It may be worth noting that in all of our actual examples we are not making use of list initialization so I hope this does not mask the actual cause.

    Many thanks for all your hard work on such a great tool!

     
  • Dan Workman

    Dan Workman - 2020-12-15

    Just found a fourth:

    #include <iostream>
    #include <vector>
    
    struct Foo
    {
      std::vector<int> bar{1,2,3};
      std::vector<int> baz{4,5,6};
    };
    
    int main()
    {
      Foo foo;
      auto itr = true ? foo.bar.begin() : foo.baz.begin();
    
      std::cout << *itr;
    }
    

    will generate:

    cppcheckWarnings.cpp:16:37: style: Same value in both branches of ternary operator. [duplicateValueTernary]
      auto itr = true ? foo.bar.begin() : foo.baz.begin();
                                        ^
    
     
  • Daniel Marjamäki

    Thanks!

    The first FP : https://trac.cppcheck.net/ticket/10051 -- there seems to be a problem with the function pointer

     
  • Daniel Marjamäki

    Second FP: https://trac.cppcheck.net/ticket/10052 -- I guess we should update the check

     
  • Daniel Marjamäki

    The last FP: https://trac.cppcheck.net/ticket/10053

    Did I miss a FP?

     
  • Dan Workman

    Dan Workman - 2020-12-18

    Thanks Daniel! I think that covers it - if you need any further information feel free to contact to me

     

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.