A resource is released with the function for another kind of resource

tx0/c:release-mismatchdefaultwarningbug, cwe-762

What it finds

f = fopen(path, "r"); ... free(f); or p = malloc(n); ... fclose(p);: each kind of resource has its own function to release it, fclose for fopen, pclose for popen, closedir for opendir and free for the allocation functions, and the wrong one corrupts memory or leaks the resource.

The variable must be local to the function and every value assigned to it there must come from functions of the same kind.

From the rule's source, release_mismatch.rs.

Example from the rule's tests
#include <stdio.h>#include <stdlib.h> void close_all(const char *path, int n){  FILE *f = fopen(path, "r");  char *p = malloc(n);  FILE *g = fopen(path, "w");  free(f);tx0 reports: f comes from fopen but is released with free  fclose(p);tx0 reports: p comes from malloc but is released with fclose  fclose(g);}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/c:release-mismatch" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/c:release-mismatch] reason="..."

tx0 explain tx0/c:release-mismatch shows the same in the terminal. Found a false positive? Report it.