Arithmetic on int constants overflows the range of int

tx0/c:constant-overflowdefaultwarningbug, cwe-190

What it finds

365 * 24 * 60 * 60 * 1000: every operand is an int, so the product is computed in int and overflows before it is assigned to anything wider, which is undefined behavior and in practice wraps to a wrong value. A suffix such as 1000LL on one operand makes the arithmetic wide enough.

Only sums, differences and products of decimal literals without suffixes count, whose type is int whatever the platform; the expression reported is the first one whose operands fit in int and whose result does not.

From the rule's source, constant_overflow.rs.

Example from the rule's tests
long long millis(void){  long long year = 365 * 24 * 60 * 60 * 1000;tx0 reports: this int arithmetic gives 31536000000  long long ok = 365LL * 24 * 60 * 60 * 1000;  int day = 24 * 60 * 60;  return year + ok + day + 0x7fffffff * 2;}
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 in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/c:constant-overflow" = "off"

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:constant-overflow] reason="..."

tx0 explain tx0/c:constant-overflow shows the same in the terminal. Found a false positive? Report it.