Do not use the assignment operator in the following contexts because it contexts listed in the following table because doing so typically indicates programmer error and can result in unexpected behavior:.
Operator | Context |
---|---|
if | controlling Controlling expression |
while | controlling Controlling expression |
do ... while | controlling Controlling expression |
for | second Second operand |
?: | first First operand |
?: | second Second or third operands, where the ternary expression is used in any of these contexts |
&& | either Either operand |
|| | either operand |
, operator | second Second operand, when the comma expression is used in any of these contexts |
...
Although the intent of the code may be to assign b
to a
and test the value of the result for equality to 0, it is very frequently a case of the programmer mistakenly using the assignment operator =
instead of the equals operator ==
. Consequently, many compilers will warn about this condition, making this coding error detectable by adhering to MSC00-C. Compile cleanly at high warning levels.
...
When the assignment is intended, the following is an alternative compliant solutionthis compliant solution explicitly uses inequality as the outermost expression while performing the assignment in the inner expression:
Code Block | ||||
---|---|---|---|---|
| ||||
if ((a = b) != 0) { /* ... */ } |
...
Noncompliant Code Example
In this noncompliant code example, the expression x = y
is used as the controlling expression of the while
statement:
Code Block | ||||
---|---|---|---|---|
| ||||
do { /* ... */ } while (foo(), x = y); |
The same result can be obtained using the for
statement, which is specifically designed to evaluate an expression on each iteration of the loop, just before performing the test in its controlling expression:
Code Block | ||||
---|---|---|---|---|
| ||||
for (; x; foo(), x = y) { /* ... */ } |
Compliant Solution (Unintentional Assignment)
When the assignment of y to x is not intended, this conditional block is now executed when x is equal to y, as in this compliant solution:
Code Block | ||||
---|---|---|---|---|
| ||||
do { /* ... */ } while (foo(), x == y); |
...
When the assignment is intended, the following is an alternative this compliant solution can be used:
Code Block | ||||
---|---|---|---|---|
| ||||
do { /* ... */ } while (foo(), (x = y) != 0); |
...
Code Block | ||||
---|---|---|---|---|
| ||||
do { /* ... */ } while (x = y, p = q); |
Compliant Solution
This is a compliant example because In this compliant solution, the expression x = y
is not used as the controlling expression of the while
statement:
...
EXP45-EX1: Assignment can be used where the result of the assignment is itself a parameter an operand to a comparison expression or relational expression. In this compliant example, the expression x = y
is itself a parameter an operand to a comparison operation:
...
EXP45-EX2: Assignment can be used where the expression consists of a single primary expression. In this compliant example, The following code is compliant because the expression x = y
is a single primary expression:
Code Block | ||||
---|---|---|---|---|
| ||||
if ((x = y)) { /* ... */ } |
EXP45-EX3: Assignment can be used in the above contexts if it occurs in a function argument or array index. In this compliant example, the expression x = y
is used in a function argument:
Code Block | ||||
---|---|---|---|---|
| ||||
if (foo(x = y)) { /* ... */ } |
...
The following controlling expression is noncompliant because &&
is not a comparison or relational operator and the entire expression is not primary:
Code Block | |
---|---|
|
...
| |||
if ((v = w) && flag) { /* ... */ } |
When the assignment of v
...
to w
...
is not intended,
...
the following controlling expression can be used to execute the conditional block
...
when v
...
is equal to w
:
Code Block | ||||
---|---|---|---|---|
| ||||
if ((v == w) && flag) { /* ... */ }; |
...
When the assignment is intended, the following
...
controlling expression can be used:
Code Block | ||||
---|---|---|---|---|
| ||||
if (((v = w) != 0) && flag) { /* ... */ }; |
EXP45-EX3: Assignment can be used in a function argument or array index. In this compliant solution, the expression x = y
is used in a function argument:
Code Block | ||||
---|---|---|---|---|
| ||||
if (foo(x = y)) { /* ... */ } |
Risk Assessment
Errors of omission can result in unintended program flow.
...
CERT C++ Secure Coding Standard | EXP19-CPP. Do not perform assignments in conditional expressions |
CERT Oracle Secure Coding Standard for Java | 53. Do not perform assignments in conditional expressions |
ISO/IEC TR 24772:2013 | Likely Incorrect Expression [KOA] |
ISO/IEC TS 17961 | No assignment in conditional expressions [boolasgn] |
MITRE CWE | CWE-480, Use of incorrect operatorIncorrect Operator |
Bibliography
[Hatton 1995] | Section 2.7.2, "Errors of Omission and Addition" |
...