CWE Rule 481
Description
Rule Description
The code uses an operator for assignment when the intention was to perform a comparison.
Polyspace Implementation
The rule checker checks for Invalid use of = (assignment) operator.
Examples
Invalid use of = (assignment) operator
This issue occurs when an
assignment is made inside the predicate of a conditional, such as if
or while
.
In C and C++, a single equal sign is an assignment not a comparison. Using a single equal sign in a conditional statement can indicate a typo or a mistake.
Conditional statement tests the wrong values— The single equal sign operation assigns the value of the right operand to the left operand. Then, because this assignment is inside the predicate of a conditional, the program checks whether the new value of the left operand is nonzero or not NULL.
Maintenance and readability issues — Even if the assignment is intended, someone reading or updating the code can misinterpret the assignment as an equality comparison instead of an assignment.
If the assignment is a bug, to check for equality, add a second equal sign (
==
).If the assignment inside the conditional statement was intentional, to improve readability, separate the assignment and the test. Move the assignment outside the control statement. In the control statement, simply test the result of the assignment.
If you do not want to fix the issue, add comments to your result or code to avoid another review. See:
Address Results in Polyspace User Interface Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
#include <stdio.h> void bad_equals_ex(int alpha, int beta) { if(alpha = beta) //Noncompliant { printf("Equal\n"); } }
The equal sign is flagged as a defect because
the assignment operator is used within the predicate of the if-statement.
The predicate assigns the value beta
to alpha
,
then implicitly tests whether alpha
is true or
false.
One possible correction is adding an additional
equal sign. This correction changes the assignment to a comparison.
The if condition compares whether alpha
and beta
are
equal.
#include <stdio.h> void equality_test(int alpha, int beta) { if(alpha == beta) { printf("Equal\n"); } }
if
Condition
If an assignment must be made inside the predicate,
a possible correction is adding an explicit comparison. This correction
assigns the value of beta
to alpha
,
then explicitly checks whether alpha
is nonzero.
The code is clearer.
#include <stdio.h> int assignment_not_zero(int alpha, int beta) { if((alpha = beta) != 0) { return alpha; } else { return 0; } }
if
Statement
If the assignment can be made outside the control
statement, one possible correction is to separate the assignment and
comparison. This correction assigns the value of beta
to alpha
before
the if. Inside the if-condition, only alpha
is
given to test if alpha
is nonzero or not NULL.
#include <stdio.h> void assign_and_print(int alpha, int beta) { alpha = beta; if(alpha) { printf("%d", alpha); } }
Check Information
Category: Others |
Version History
Introduced in R2023a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)