calloc is given the size of an element first and the number of elements second
What it finds
calloc(sizeof(struct item), count): calloc takes the number of elements first and the size of one second. The product is the same, but the order is what tools that check bounds read, and it says what the call allocates. The fix swaps the arguments.
The first argument must be a sizeof and the second neither a sizeof nor a constant.
From the rule's source, calloc_size_first.rs.
#include <stdlib.h> struct item { int v; }; struct item *make(size_t count){ struct item *a = calloc(sizeof(struct item), count);tx0 reports: the size of an element comes first here struct item *b = calloc(count, sizeof(struct item)); struct item *c = calloc(sizeof(struct item), 1); free(a); free(c); return b;} 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:calloc-size-first" = "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:calloc-size-first] reason="..."tx0 explain tx0/c:calloc-size-first shows the same in the terminal. Found a false positive? Report it.