Menu

cppcheck does not detect memset on struct with std container when size is multiplied

2018-06-04
2018-06-08
  • Alexey Popov

    Alexey Popov - 2018-06-04

    Example code:

    struct A_TEST
    {
       int m_nNumber;            
       char m_szID[5];
       std::vector<int> m_vStrings;
    };
    
    int main(int)
    {
       std::vector<A_TEST> vec_A(5);
       memset(&vec_A[0], 0, sizeof(A_TEST) * vec_A.size());
    
       std::vector<A_TEST> vec_B(5);
       memset(&vec_B[0] , 0 , sizeof(A_TEST));
    }
    

    Cppcheck doesn't detect memset in line 11 - only in line 14:

    cppcheck --enable=style --inconclusive --inline-suppr -j 4 --suppress=functionStatic --suppress=syntaxError --suppress=noExplicitConstructor --quiet . 2>&1
    [main.cpp:14]: (error) Using 'memset' on struct that contains a 'std::vector'.
    
     
  • raynebc

    raynebc - 2018-06-05

    One of the code validation programs I use complains if I declare variables after program code (ie. call to memset() ) in a code block. If you declare both vectors and then make both calls to memset() afteward, does Cppcheck still only warn about the second use of memset()?

     
  • Alexey Popov

    Alexey Popov - 2018-06-07

    It is not working either. Warns about the second use of memset only:

    struct A_TEST
    {
       int m_nNumber;
       char m_szID[5];
       std::vector<int> m_vStrings;
    };
    
    int main(int)
    {
       std::vector<A_TEST> vec_A(5);
       std::vector<A_TEST> vec_B(5);
    
       memset(&vec_A[0], 0, sizeof(A_TEST) * vec_A.size());
       memset(&vec_B[0] , 0 , sizeof(A_TEST));
    }
    
    $ cppcheck --enable=style --inconclusive --inline-suppr -j 4 --suppress=functionStatic --suppress=syntaxError --suppress=noExplicitConstructor --quiet . 2>&1
    [main.cpp:14]: (error) Using 'memset' on struct that contains a 'std::vector'.
    
     
  • versat

    versat - 2018-06-08

    Thanks for the report, it really looks like a false negative to me.
    I created ticket 8619 for this issue and modified/reduced the code a bit but it should still show exactly the issue you detected.

     
  • Alexey Popov

    Alexey Popov - 2018-06-08

    Thank you too.

     

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.