Menu

Nullptr dereference false positive with variadic function?

2021-08-27
2021-10-18
  • Mark Bourgeault

    Mark Bourgeault - 2021-08-27

    I'm on cppcheck 2.3. I apologize in advance if this is already fixed...it's not trivial for me to upgrade my environment.

    Test program, a.cpp:

    #include <cstdlib>
    
    template <typename... Args>
    void fatalError(int code, const char* fmt, Args&&... args) { exit(code); }
    
    int main(int argc, char* argv[]) {
        int* ptr = argc == 2 ? new int : nullptr;
        if (!ptr) {
            fatalError(1, "%s", argv[0]);
            /* fatalError(1, "%d", argc); // uncomment line to bypass false +ve */
        }
        *ptr;
        return 0;
    }
    

    cppcheck --enable=warning a.cpp results in a nullPointerRedundantCheck warning. Uncommenting the line results in no warnings.

    At this point, I'm pretty confident that it's not my error. Thanks in advance for any help.

     

    Last edit: Mark Bourgeault 2021-08-27
    • Daniel Marjamäki

      I can reproduce with cppcheck-2.3 and cppcheck-2.4. but it's fixed in cppcheck-2.5

      I'm on cppcheck 2.3. I apologize in advance if this is already fixed...it's not trivial for me to upgrade my environment.

      no problem, thanks for reporting.

       
      • Mark Bourgeault

        Mark Bourgeault - 2021-08-27

        There is a variation involving namespaces that results in a false positive on 2.5.

        #include <cstdlib>
        
        namespace ns {
        template <typename... Args>
        void fatalError(int code, const char* fmt, Args&&... args) { exit(code); }
        }
        
        int main(int argc, char* argv[]) {
        int* ptr = argc == 2 ? new int : nullptr;
        if (!ptr) {
            using namespace ns;
            fatalError(1, "%s", argv[0]);
            /* ns::fatalError(1, "%s", argv[0]); // uncomment line to bypass false +ve */
        }
        *ptr;
        return 0;
        }
        
         

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.