free is given memory that was not allocated on the heap

tx0/c:free-non-heapdefaultwarningbug

What it finds

free(&x), free(buf) for an array buf, free("text"): memory that malloc did not return cannot be freed, and passing it to free corrupts the heap or crashes.

Counted are the address of a variable, of one of its fields or of an element of an array, an array declared in the file with its brackets in its own declarator and not as a parameter, which would be a pointer, and string literals. The function must be the C library's free.

From the rule's source, free_non_heap.rs.

Example from the rule's tests
#include <stdlib.h> struct item {  char name[16];  char *data;}; static char table[64]; void release(struct item *it, char *p, char param[8]){  struct item local;  char buf[32];  free(&local);tx0 reports: free is given the address of local, which was not allocated with malloc  free(buf);tx0 reports: free is given the array buf  free(table);tx0 reports: free is given the array table  free(&buf[0]);tx0 reports: free is given the address of buf  free(&local.name);tx0 reports: free is given the address of local  free("constant");tx0 reports: free is given a string literal  free(&p);tx0 reports: free is given the address of p  free(p);  free(it->data);  free(&p[0]);  free(param);  free(it->name);}
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:free-non-heap" = "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:free-non-heap] reason="..."

tx0 explain tx0/c:free-non-heap shows the same in the terminal. Found a false positive? Report it.