A loop over an array counts to its size in bytes, which is more than its number of elements
What it finds
int counts[16]; for (i = 0; i < sizeof counts; i++) counts[i] = 0;: sizeof gives the size of the array in bytes, 64 here, not its 16 elements, and the loop runs far past the end. The count of elements is sizeof counts / sizeof counts[0].
The loop must count up a variable that its condition compares with sizeof of an array the file declares, not a parameter, and the body must index that array with the variable. The elements must be wider than a byte as the declaration shows: integers of a known width above eight bits, floating values, structs, unions, pointers or arrays.
From the rule's source, byte_size_as_loop_count.rs.
#include <stddef.h> static int counts[16];static double weights[8]; void reset(void){ for (size_t i = 0; i < sizeof counts; i++)tx0 reports: sizeof counts counts the bytes of counts, not its elements counts[i] = 0; for (size_t i = 0; i < sizeof(weights); ++i)tx0 reports: sizeof weights counts the bytes of weights weights[i] = 1.0;} void reset_right(void){ char name[16]; unsigned char raw[4]; for (size_t i = 0; i < sizeof counts / sizeof counts[0]; i++) counts[i] = 0; for (size_t i = 0; i < sizeof name; i++) name[i] = 0; for (size_t i = 0; i < sizeof raw; i++) raw[i] = 0;} void bytes(void){ unsigned char *b = (unsigned char *)counts; for (size_t i = 0; i < sizeof counts; i++) b[i] = 0;} 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:byte-size-as-loop-count" = "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:byte-size-as-loop-count] reason="..."tx0 explain tx0/c:byte-size-as-loop-count shows the same in the terminal. Found a false positive? Report it.