A product of int values is stored in a wider type after it may already have overflowed

tx0/c:narrow-product-widenedrecommendedwarningreliability/arithmeticdepth fastbug, cwe-190

What it finds

long bytes = width * height; with int width, height: the product is computed in int and overflows there before it is widened, so the wider variable does not help. Converting one operand first, (long)width * height, computes it in the wide type.

Both operands must be variables declared as int or narrower in the file, and the variable stored to long, long long, size_t or a 64-bit type name.

From the rule's source, narrow_product_widened.rs.

Example from the rule's tests
#include <stddef.h> long area(int width, int height, long scale){  long bytes = width * height;tx0 reports: this product is computed in int and may overflow before it is stored in bytes  size_t n;  int small = width * height;  long fine = (long)width * height;  n = width * height;tx0 reports: stored in n  return bytes + (long)n + small + fine + scale * width;}
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 from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/c:narrow-product-widened" = "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:narrow-product-widened] reason="..."

tx0 explain tx0/c:narrow-product-widened shows the same in the terminal. Found a false positive? Report it.