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

tx0/cpp:sizeof-pointer-allocationdefaultwarningreliability/memory-safetydepth fastbug, 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.

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/cpp: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/cpp:sizeof-pointer-allocation] reason="..."

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