Rodri - 2026-07-30

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

Cppcheck reports resourceLeak for the original program, but it misses a semantically similar case when the control flow reaches the return statement through a goto label.

Original seed program

#include <stdio.h>

int main()
{
    const FILE *a = fopen("good.c", "r");
    if (!a)
        return 0;

    return 0;
}

Cppcheck reports:

<error id="resourceLeak" severity="error" msg="Resource leak: a" ...>

Reproducing program

#include <stdio.h>

int main() {
  const FILE *a;
  a = fopen("good.c", "r");
  goto check;
check:
  if (!a)
    return 0;
  return 0;
}

In this program, if fopen succeeds, a is returned without being closed by fclose(a), so the file resource is leaked.

Actual result

Cppcheck reports no resourceLeak warning for the reproducing program.

Expected result

Cppcheck should report resourceLeak for a.

Verification

The issue was reproduced with:

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

Version: 2.21.0