Assignment used as a condition

ID

c.correctness.assignment_in_condition

Severity

low

Remediation Complexity

trivial

Remediation Risk

medium

Remediation Effort

low

Resource

Suspicious Construct

Language

C / C++

Description

An assignment (=) is used as the controlling expression of this condition, where a comparison (==) was likely intended. If the assignment is deliberate, make the intent explicit by comparing the result (e.g. if ((x = y) != 0)); otherwise use ==.

Rationale

An assignment (=) is used as the controlling expression of this condition, where a comparison (==) was likely intended. If the assignment is deliberate, make the intent explicit by comparing the result (e.g. if ((x = y) != 0)); otherwise use ==.

The following code illustrates the pattern detected by this rule:

void f(int x_m1, int x_m2)
{
    // FLAGGED: Assignment used as a condition
    if (x_m1 = x_m2) {
        /* ... */
    }

Remediation

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

Configuration

This detector does not need any configuration.