An unsigned value is negated, which gives a large positive number

tx0/c:unsigned-negationstrictwarningreliability/arithmeticdepth fastbug, cwe-191

What it finds

offset = -length; with unsigned length: negating an unsigned value gives a large positive number, 2^N minus the value, not a negative one. Code that means a negative offset needs a signed type first.

The operand must be a variable declared unsigned in the file, of at least 32 bits, since narrower ones are promoted to int and negate as expected. Code that turns a magnitude into a negative signed number writes -v on purpose and converts the result, so the rule is strict.

From the rule's source, unsigned_negation.rs.

Example from the rule's tests
long back(unsigned length, int n, unsigned char c){  long offset = -length;tx0 reports: length is unsigned, so its negation is a large positive number  return offset - n + -c;}
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 strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

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

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