Menu

False positive nullPointerRedundantCheck when reusing FILE handles

mcandre
2020-11-23
2020-11-23
  • mcandre

    mcandre - 2020-11-23

    When reusing file handles, such as for a REPL, then cppcheck presents what are possibly spurious warnings between these two code sections:

                FILE *f = NULL;
    
                switch (command) {
                case 'l':
                    if (f != NULL && fclose(f) == EOF) {
                        fprintf(config->console_err, "error closing file\n");
                        return EXIT_FAILURE;
                    }
    
    #if defined(_MSC_VER)
                     if (fopen_s(f, content, "rb") != 0) {
                        fprintf(stderr, "error opening file %s\n", content);
                        return EXIT_FAILURE;
                    }
    #else
                    f = fopen(content, "rb");
    
                    if (f == NULL) {
                        fprintf(stderr, "error opening file %s\n", content);
                        return EXIT_FAILURE;
                    }
    #endif
                ...
    
     
  • mcandre

    mcandre - 2020-11-23

    Update:

    Lol, I was mistakenly sending a single pointer of the FILE to fopen_s, where fopen_s actually expects a double pointer. The linter ignores the type mismatch and proceeds to blast through into generic null pointer warnings.

     

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.