The result of a function that allocates is discarded, so it can never be released
What it finds
malloc(n); or fopen(path, "r"); as a statement of its own: the memory or the file is allocated and its only handle thrown away, so it can never be freed or closed. For realloc, the block may also have moved, leaving the old pointer invalid.
Only the C library's functions count: a file that declares one of them, or defines a macro of the name, is left alone.
From the rule's source, discarded_allocation.rs.
#include <stdio.h>#include <stdlib.h> void leak(const char *path, int n){ char *kept = malloc(n); malloc(n);tx0 reports: the result of malloc is discarded, so the memory can never be freed fopen(path, "r");tx0 reports: the result of fopen is discarded, so the file can never be closed free(kept);} 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:
[rules]
"tx0/c:discarded-allocation" = "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:discarded-allocation] reason="..."tx0 explain tx0/c:discarded-allocation shows the same in the terminal. Found a false positive? Report it.