Two ints are multiplied and the result is stored in a long, after it may already have overflowed

tx0/java:int-product-widenedrecommendedwarningbug

What it finds

long bytes = count * size; multiplies two ints in int arithmetic and widens the result to long only afterwards, so a product above 2^31 has already wrapped around. Casting one operand, as in (long) count * size, makes the multiplication itself use long, as the promotion rules of the language specification say.

Reported is a long variable initialized with, or assigned, a product of operands of type int or narrower that reads at least one variable.

From the rule's source, int_product_widened.rs.

Example from the rule's tests
class Sizes {  long total(int count, int size, long big) {    long a = count * size;tx0 reports: count * size is computed in int    long b = (long) count * size;    long c = 60 * 60;    long d = big * size;    a = count * 1024;tx0 reports: computed in int    return a + b + c + d;  }}
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/java: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/java:int-product-widened] reason="..."

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