Skip to content

False-positive: fopen() without fclose() in main() reported as a "resource leak" clang:static analyzer #133942

Open
@alavrentiev

Description

@alavrentiev
int main(int argc, char** argv)
{
    int   port;
    FILE* fp;

    /* Cmd.-line args */
    if (argc != 4  ||
        (fp = fopen(argv[1], "r")) == 0  ||
        ((port = atoi(argv[3])) & ~0xFFFF)) {
        fputs("\nUSAGE:\n"
              "test_fw <inp_file> <host> <port>\n\n", stderr);
        return 1;  // <== Opened stream never closed. Potential resource leak
    }
   ...
   /* some work is done here */
   ...
}

Return from main() with an open file does not pose any resource leaks, as the file gets closed by C RTL right away.
It if was any other function, then yes, in such a situation the report would be justified and true, but not for main().

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions