Memory is used through a pointer after the pointer was freed

tx0/c:use-after-freedefaultwarningbug, cwe-416

What it finds

free(p); and then p->next or *p a few statements later in the same block: the memory is read or written after it was given back, while the allocator may already have handed it out again.

The statements in between must be expression statements or declarations that do not mention p and call only functions of the C library, so that nothing can have given p a new value or left the block; p must be a local variable or parameter whose address is never taken. The first statement after the call that mentions p is the one looked at: if it assigns p a new value first, all is well.

From the rule's source, use_after_free.rs.

Example from the rule's tests
#include <stdlib.h>#include <string.h> struct node { struct node *next; int value; }; int after(struct node *n, char *buf, int k){  free(n);  memset(buf, 0, 4);  return n->value;tx0 reports: n is used after it was freed} void walk(struct node *n){  free(n);  n = n->next;tx0 reports: n is used after it was freed} void fine(struct node *n, struct node *m, struct node *o){  free(n);  n = NULL;  free(m);  m = malloc(sizeof(*m));  m->value = 1;  free(o);  if (o)    o->value = 2;}
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:use-after-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:use-after-free] reason="..."

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