Unsigned value compared against a negative literal

ID

c.correctness.unsigned_compared_to_negative

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Numeric Operation

Language

C / C++

Description

An unsigned value is compared against a negative literal. An unsigned type can never hold a negative value, so this comparison is always true or always false regardless of the unsigned operand — the condition does not test what it appears to test. Compare against 0, or change the operand to a signed type if negative values are actually meaningful here.

Rationale

An unsigned value is compared against a negative literal. An unsigned type can never hold a negative value, so this comparison is always true or always false regardless of the unsigned operand — the condition does not test what it appears to test. Compare against 0, or change the operand to a signed type if negative values are actually meaningful here.

The following code illustrates the pattern detected by this rule:

void configure(unsigned int u, unsigned long ul, int s)
{
    // FLAGGED: Unsigned value compared against a negative literal
    if (u < -1) { }

    // FLAGGED: Unsigned value compared against a negative literal
    if (u == -1) { }

    // FLAGGED: Unsigned value compared against a negative literal
    if (ul > -5) { }

    if (u >= 0) { }

    if (u > 5) { }

    if (s < -1) { }
}

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.

References

© 2026 Xygeni. All rights reserved