A pointer that a test found NULL on some path is dereferenced on that path

tx0/c:null-on-some-pathstrictwarningreliability/null-safetydepth fastbug, cwe-476

What it finds

if (p == NULL) log("missing"); ... p->len: the code tests a pointer for NULL, and a path on which the test found it NULL goes on to dereference it. Either the test or the missing return after it is wrong.

``c if (cfg == NULL) fprintf(stderr, "no configuration\n"); size = cfg->size; // crashes when cfg is NULL ``

The rule follows each function on its paths as null-dereference does, and reports a dereference that is NULL on some of the paths that reach it, but not on all; those are null-dereference's. The NULL comes from a test or from the constant. Conditions that tie the pointer to another variable, as len = p ? strlen(p) : 0 does, are not followed, so some reports are paths that cannot happen; the rule is in the strict tier.

From the rule's source, null_on_some_path.rs.

Example from the rule's tests
#include <stdio.h>#include <stddef.h> struct config { int size; }; int size_of(struct config *cfg){  if (cfg == NULL)    fprintf(stderr, "no configuration\n");  return cfg->size;tx0 reports: cfg may be NULL here} int checked(struct config *cfg){  if (cfg == NULL)    return 0;  return cfg->size;} int same_test(struct config *cfg, int k){  int size = 0;  if (cfg != NULL)    size = 1;  if (cfg != NULL)    size = cfg->size;  return size + k;}
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 from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/c:null-on-some-path" = "warning"

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:null-on-some-path] reason="..."

tx0 explain tx0/c:null-on-some-path shows the same in the terminal. Found a false positive? Report it.