An unsigned value is tested for being negative
What it finds
u < 0 and u >= 0 for an unsigned u: an unsigned value is never negative, so the first is always false and the second always true. Usually the variable was meant to be signed, as when a loop counts down with for (i = n - 1; i >= 0; i--), which never ends.
The type must be certainly unsigned from a declaration in the file: written with unsigned or bool, or as size_t or one of the unsigned types of stdint.h, or as a type name declared in the file as one of those. A type name from a header, whose signedness may change with the platform, does not count.
From the rule's source, unsigned_zero_comparison.rs.
#include <stddef.h>#include <stdbool.h> typedef unsigned int count_t; struct dev { unsigned int irq; int fd;}; int check(struct dev *d, size_t n, unsigned u, count_t c, bool b, int i, ssize_t s, u32 k){ int bad = 0; if (d->irq < 0)tx0 reports: d->irq is unsigned, so this comparison is always false bad++; if (n >= 0)tx0 reports: n is unsigned, so this comparison is always true bad++; if (0 > u)tx0 reports: u is unsigned, so this comparison is always false bad++; if (0 <= c)tx0 reports: c is unsigned, so this comparison is always true bad++; if (b < 0)tx0 reports: b is unsigned, so this comparison is always false bad++; for (unsigned j = n; j >= 0; j--)tx0 reports: j is unsigned, so this comparison is always true bad++; if (d->fd < 0) bad++; if (i < 0 || s < 0 || k < 0) bad++; if (u > 0 || u <= 0 || 0U < 1) bad++; if ((int)u < 0) bad++; return bad;} 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:
[rules]
"tx0/c:unsigned-zero-comparison" = "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:unsigned-zero-comparison] reason="..."tx0 explain tx0/c:unsigned-zero-comparison shows the same in the terminal. Found a false positive? Report it.