Menu

a case of CWE: 401 Memory leak

AlexHsieh
2024-01-08
2024-01-08
  • AlexHsieh

    AlexHsieh - 2024-01-08

    Hi,
    I use cppcheck-2.13.0 to check the following code

    #include <list>
    using namespace std;
    
    struct C{
        char* m_p;
        C(char* p) : m_p(p){}
    };
    
    int main(){
        std::list<C> li;
        char* p = new char[1];
        C c(p);
        li.push_back(c);
        C c2(li.front());
        delete [] c2.m_p;
    }
    

    , and get

    CWE: 401
    Memory leak: p

    but I think the memery assigned to p (char* p = new char[1])
    is deleted by "delete [] c2.m_p".
    Is this a false positive?

     
  • CHR

    CHR - 2024-01-08

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/12327

     

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.