Two int values are multiplied and the product is stored as long, after it may have overflowed

tx0/csharp:int-product-widenedrecommendedwarningreliability/arithmeticdepth fastbug

What it finds

long total = a * b; multiplies in int and widens the result afterwards, so a product above int.MaxValue has already wrapped around when it is stored. Casting one operand first, (long)a * b, multiplies in 64 bits. The rule reports a multiplication or left shift whose operands are both declared int (or are int literals, but not both) and whose result initializes or is assigned to a variable declared long or ulong.

From the rule's source, int_product_widened.rs.

Example from the rule's tests
class Sizes{    long Bytes(int count, int size)    {        long total = count * size;tx0 reports: count * size is computed in int and may overflow before it is widened        long wide = (long)count * size;        long small = 4 * 1024;        total = count * 1024;tx0 reports: count * 1024 is computed in int and may overflow before it is widened        return total + wide + small;    }}
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/csharp:int-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/csharp:int-product-widened] reason="..."

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