Menu

False negative?

Ken Ajiro
2017-03-04
2017-03-04
  • Ken Ajiro

    Ken Ajiro - 2017-03-04

    I tried cppcheck with following source, however cppcheck did not report memory leak depending on condition.

    #include <stdlib.h>
    static int foo = 1;
    static int bar = 1;
    
    static void
    func()
    {
    }
    
    void
    main()
    {
        char *p = malloc(10);
        if (p) {
            if (foo) {
                if (bar) {
    #ifdef COND
                    func();
    #endif
                }
                return;
            }
            free(p);
        }
    }
    
    ken@ubuntu:~$ cppcheck --version
    Cppcheck 1.72
    ken@ubuntu:~$ cppcheck -DCOND test.c
    Checking test.c...
    ken@ubuntu:~$ cppcheck -UCOND test.c
    Checking test.c...
    [test.c:21]: (error) Memory leak: p
    

    It looks like false negative...

     
  • Daniel Marjamäki

    Latest cppcheck warns:

    daniel@debian:~/cppcheck$ ./cppcheck 1.c
    Checking 1.c ...
    [1.c:20]: (error) Memory leak: p
    Checking 1.c: COND...
    

    Strangely ... latest cppcheck does not warn if -DCOND is used.

     

    Last edit: Daniel Marjamäki 2017-03-04

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.