Menu

why does cppcheck wont report any error if there is a miss in close() system cal

2016-04-20
2020-01-14
  • pavithra rani

    pavithra rani - 2016-04-20

    Hi,

    I can find cppcheck throwing Resource Leak whenever there is a fopen() call is made but fclose is missing.
    #include <stdio.h>

    include <stdlib.h>

    int main()
    {
    FILE * fp;

    fp = fopen ("file.txt", "w+");
    fprintf(fp, "%s %s %s %d", "We", "are", "in", 2016);
    return(0);
    }

    Results: (error) Resource leak: fp
    fclose(fp) is missing.
    but

    define FNAME "a.txt"

    int main()
    {
    int fd;
    fd=open(FNAME,O_RDONLY,0);
    return 0;
    }

    Cppcheck wont throw any error as resource leak.

    please reply if any one is aware of this.

    Thanks,
    Pavithra

     
  • Alexey Dokuchaev

    By default, cppcheck does not catch open() leaks for some reason, but it will if invoked with --library=posix switch.

     
  • Daniel Marjamäki

    Yes, use all --library options that apply. The --library=posix can't be loaded by default because there are many users that don't target posix systems.

     

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.