The size given to malloc or realloc is a product that may overflow unchecked
What it finds
malloc(count * size): if the product wraps around, a small block is allocated and the code that fills it writes far past its end. calloc(count, size) checks the multiplication, and so does an explicit test before it.
A product of constants and sizeof only is left alone.
From the rule's source, allocation_size_product.rs.
#include <stdlib.h> void *table(unsigned long rows, unsigned long cols){ void *a = malloc(rows * cols);tx0 reports: the size given to malloc is a product that is not checked for overflow void *b = malloc(16 * sizeof(int)); void *c = calloc(rows, cols); free(b); free(c); return a;} 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:
[rules]
"tx0/c:allocation-size-product" = "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:allocation-size-product] reason="..."tx0 explain tx0/c:allocation-size-product shows the same in the terminal. Found a false positive? Report it.