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:
int get_expr(void);
void f(int x_m1, int x_m2)
{
// FLAGGED: Assignment used as a condition
if (x_m1 = x_m2) {
/* ... */
}
if (x_m1 == x_m2) {
/* ... */
}
int r;
// FLAGGED: Assignment used as a condition
while (r = get_expr()) {
/* ... */
}
// Deliberate assignment made explicit with a comparison.
while ((r = get_expr()) != 0) {
/* ... */
}
}
Remediation
Follow secure coding practices and review the references below for detailed remediation guidance.