Memory is freed twice in a row

tx0/c:double-freedefaultwarningbug

What it finds

free(p); ... free(p); in a row: the second call frees memory that is already free, which corrupts the heap.

Both calls must be statements of the same block, and p a local variable or parameter whose address is never taken in the function. The statements between them may not mention p, carry a label, hold a directive, or call anything but functions of the C library, since a function of the program may not return or a macro may do anything.

From the rule's source, double_free.rs.

Example from the rule's tests
#include <stdlib.h>#include <string.h> void cleanup(void);void reset(char **pp); void twice(char *p, char *q, int n){  free(p);  free(p);tx0 reports: p is freed a second time   free(q);  memset(&n, 0, sizeof(n));  free(q);tx0 reports: q is freed a second time} void fine(char *p, char *q, char *r, char *s, int n){  free(p);  p = malloc(4);  free(p);   free(q);  cleanup();  free(q);   free(r);  if (n)    r = NULL;  free(r);   free(s);again:  free(s);  if (n--)    goto again;} void through_address(char *p){  char **pp = &p;  free(p);  reset(pp);  free(p);} void branches(char *p){  free(p);#ifdef REALLOC  p = malloc(1);#endif  free(p);}
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:double-free" = "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:double-free] reason="..."

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