Menu

False Positive : Condition is always true with reset/release on unique_ptr

2020-06-16
2020-06-17
  • ttressieres

    ttressieres - 2020-06-16

    Hi,

    The following code exhibit the problem

    int* df(int* expr);
    
    int * level()
    {
        std::unique_ptr<int> fixedLeg;
        fixedLeg.reset(df(fixedLeg.release()));
        if (!fixedLeg)
            return nullptr;
        return fixedLeg.release();
    }
    

    library_release.cpp:7:9: style: Condition '!fixedLeg' is always true [knownConditionTrueFalse]
    if (!fixedLeg)

    If on line 6, I replace release() by get(), I receive no warning

    int* df(int* expr);
    
    int * level()
    {
        std::unique_ptr<int> fixedLeg;
        fixedLeg.reset(df(fixedLeg.get()));
        if (!fixedLeg)
            return nullptr;
        return fixedLeg.release();
    }
    
     
  • ttressieres

    ttressieres - 2020-06-16

    False positive with cppcheck 2.0 and 2.1

     
  • Daniel Marjamäki

    Thanks! I can reproduce. I created https://trac.cppcheck.net/ticket/9777

     

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.