Memory from malloc is used right away without a test that the allocation succeeded

tx0/c:unchecked-allocation-userecommendedwarningbug, cwe-690

What it finds

p = malloc(n); p->len = 0;: malloc returns NULL when memory runs out, and the next statement writes through the result without looking, which crashes or, at an offset, writes to memory near address zero.

The allocation must be assigned to a local variable or parameter, or initialize one, and the very next statement must dereference it or pass it as the destination of memset, memcpy, strcpy or sprintf, in a part that always runs.

From the rule's source, unchecked_allocation_use.rs.

Example from the rule's tests
#include <stdlib.h>#include <string.h> struct s { int len; }; struct s *make(int n, const char *text){  char *copy;  struct s *p = malloc(sizeof(*p));  p->len = n;tx0 reports: p is used without a test that malloc succeeded  copy = malloc(n);  strcpy(copy, text);tx0 reports: copy is used without a test that malloc succeeded  copy = malloc(n);  if (copy == NULL)    return NULL;  copy[0] = 0;  return 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 from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/c:unchecked-allocation-use" = "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:unchecked-allocation-use] reason="..."

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