Menu

Condition '!ptr' is always true when calling std::unique_ptr::reset

2019-07-02
2019-07-05
  • ttressieres

    ttressieres - 2019-07-02

    With the following code, I receive a condition is always true at line 17

    #include <memory>
    
    int * allocate(int * in)
    {
        if (!in)
            return new int(2);
        delete in;
        return nullptr;
    }
    
    int main(int, char **)
    {
        std::unique_ptr<int> ptr(allocate(nullptr));
        if (!ptr)
            return -1;
        ptr.reset(allocate(ptr.release()));
        if (!ptr)
            return -1;
        return 0;
    }
    

    cppcheck.exe --enable=all --template=vs --library=std.cfg --library=windows.cfg -DOS_NT -DWIN32 -D_WIN32 -DNDEBUG -D_cppcheck --inline-suppr test.cpp
    Checking test.cpp ...
    Checking test.cpp: OS_NT=1;WIN32=1;_WIN32=1;NDEBUG=1;_cppcheck=1...
    test.cpp(17): style: Condition '!ptr' is always true
    nofile(0): information: Cppcheck cannot find all the include files (use --check-config for details)

    It's new with version 1.88

     
  • Paul Fultz

    Paul Fultz - 2019-07-04

    This is most likely because we do not recognize reset or release as methods that can change the variable. I wonder if its as easy as updating std.cfg about these methods.

     
  • versat

    versat - 2019-07-05

    The only configuration regarding unique_ptr in std.cfg is currently:

      <smart-pointer class-name="std::unique_ptr"/>
    

    I guess that Cppcheck should assume that variables are changed if the function is unknown/not configured. So IMHO this looks like an issue in Cppcheck.

     

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.