A local variable is read where some path to the read never assigned it

tx0/c:maybe-uninitialized-readstrictwarningreliability/memory-safetydepth fastbug, cwe-457

What it finds

int len; if (ok) len = n; return len;: on some path to the read the variable was never assigned, while on others it was. The value read on that path is indeterminate.

``c int fd; if (path != NULL) fd = open(path, O_RDONLY); return fd; // indeterminate when path is NULL ``

The rule follows each function on its paths as uninitialized-read does. Paths are kept apart while they are few, so a variable assigned under a condition and read only under the same condition is not reported; past four paths, or where the conditions are not the same values, the rule stays silent rather than guess. Conditions that are tied through other variables, as a switch on a kind that a goto path never sets, are not followed, so some reports are paths that cannot happen; the rule is in the strict tier.

From the rule's source, maybe_uninitialized_read.rs.

Example from the rule's tests
int open_file(const char *path); int descriptor(const char *path){  int fd;  if (path != 0)    fd = open_file(path);  return fd;tx0 reports: fd may be read before a value is assigned to it} int both(int k){  int x;  if (k)    x = 1;  else    x = 2;  return x;} int same_condition(int k){  int x;  int total = 0;  if (k > 2)    x = 5;  if (k > 2)    total = x;  return total;}
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:maybe-uninitialized-read" = "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:maybe-uninitialized-read] reason="..."

tx0 explain tx0/c:maybe-uninitialized-read shows the same in the terminal. Found a false positive? Report it.