A pointer is tested for NULL after it was already dereferenced

tx0/c:check-after-dereferencedefaultwarningbug, cwe-476

What it finds

n = p->len; if (p == NULL) return;: the test comes too late, since p->len has already crashed if p is NULL; either the test is not needed or it belongs before the dereference.

The dereference must be in an expression statement or declaration of a block that always runs, the test, !p or p == NULL, in an if among the next eight statements of the same block, and the statements in between may not change p, branch or jump. p must be a local variable or parameter whose address is never taken.

From the rule's source, check_after_dereference.rs.

Example from the rule's tests
#include <stddef.h> struct s { int len; }; int size(struct s *p, struct s *q){  int n = p->len;  n++;  if (p == NULL)tx0 reports: p is tested for NULL after it was dereferenced    return -1;  n += q->len;  q = NULL;  if (!q)    return n;  return n;}
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:check-after-dereference" = "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:check-after-dereference] reason="..."

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