Memory for what a pointer points to is allocated with the size of the pointer itself

tx0/c:sizeof-pointer-allocationdefaultwarningbug, cwe-131

What it finds

p = malloc(sizeof(p)) or p = malloc(n * sizeof(p)): the size is that of the pointer, 4 or 8 bytes, where the size of what it points to, sizeof(*p), was meant; the block is too small for a struct and writes run past its end.

The pointer that receives the result must be the one under sizeof, declared in the file as a pointer to something that is not a pointer, so that the two sizes differ.

From the rule's source, sizeof_pointer_allocation.rs.

Example from the rule's tests
#include <stdlib.h> struct item { int a, b, c, d; }; void make(int n){  struct item *one = malloc(sizeof(one));tx0 reports: sizeof(one) is the size of the pointer  struct item *many;  struct item **list;  many = malloc(n * sizeof(many));tx0 reports: sizeof(many) is the size of the pointer  list = malloc(n * sizeof(list));  many = malloc(n * sizeof(*many));  free(one);  free(many);  free(list);}
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:sizeof-pointer-allocation" = "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:sizeof-pointer-allocation] reason="..."

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