Menu

False negatives: moved standard-library objects are accessed after std::move

Rodri
24 hours ago
24 hours ago
  • Rodri

    Rodri - 24 hours ago

    Hi, I found false negatives regarding the rule accessMoved.

    Cppcheck misses accesses through member-function calls after moving standard-library objects in the following programs.

    Case 1: moved vector queried with empty()

    #include <iostream>
    #include <utility>
    #include <vector>
    
    void foo(std::vector<int>);
    
    int main() {
      std::vector<int> v = {1, 2, 3};
      foo(std::move(v));
      if (!v.empty()) {
        std::cout << "Error: Vector not empty";
      }
    }
    

    The vector v is moved into foo, then accessed by calling v.empty().

    Case 2: moved map queried with find()

    #include <iostream>
    #include <map>
    #include <utility>
    
    void foo(std::map<int, int>);
    
    int main() {
      std::map<int, int> m = {{1, 2}};
      foo(std::move(m));
      auto it = m.find(1);
      std::cout << it->second << std::endl;
    }
    

    The map m is moved into foo, then accessed by calling m.find(1).

    Case 3: moved string queried with size()

    #include <iostream>
    #include <string>
    #include <utility>
    
    void foo(std::string);
    
    int main() {
      std::string s = "test";
      foo(std::move(s));
      if (s.size() > 0) {
        std::cout << "Not empty";
      }
    }
    

    The string s is moved into foo, then accessed by calling s.size().

    Actual result

    Cppcheck reports no accessMoved warning for any of the three programs.

    Expected result

    Cppcheck should report accessMoved for v, m, and s when they are accessed after std::move.

    Verification

    The issue was reproduced with:

    cppcheck --output-format=xmlv2 --enable=all access_moved_vector.cpp
    cppcheck --output-format=xmlv2 --enable=all access_moved_map.cpp
    cppcheck --output-format=xmlv2 --enable=all access_moved_size.cpp
    

    Version: 2.21.0

     
  • CHR

    CHR - 24 hours ago

    The warnings appear with --inconclusive.

     

Log in to post a comment.

Auth0 Logo