Menu ▾ ▴

False negative: file resource leak with a local pointer alias

Rodri
2026-09-29
2026-09-30
  • Rodri

    Rodri - 2026-09-29

    Hi, I found a false negative regarding the rule resourceLeak.

    Cppcheck misses the resource leak when the null check uses a local alias of the file pointer.

    #include <cstdio>
    
    int f() {
      const FILE *a = fopen("input.txt", "r");
      const FILE *b = a;
      if (!b)
        return 0;
      return 0;
    }
    

    Actual result

    Cppcheck does not report resourceLeak. Removing b and changing the condition to if (!a) produces the warning.

    Expected result

    Cppcheck should report the missing fclose in both forms.

    Verification

    The issue was reproduced with:

    cppcheck --output-format=xmlv2 --enable=all file_alias.cpp
    

    Version: 2.21.0

     
  • CHR

    CHR - 2026-09-30
     

Log in to post a comment.