结构: Simple
Abstraction: Variant
状态: Draft
被利用可能性: Low
The code uses an operator for comparison when the intention was to perform an assignment.
In many languages, the compare statement is very close in appearance to the assignment statement; they are often confused.
cwe_Nature: ChildOf cwe_CWE_ID: 480 cwe_View_ID: 1000 cwe_Ordinal: Primary
cwe_Nature: ChildOf cwe_CWE_ID: 480 cwe_View_ID: 699 cwe_Ordinal: Primary
Language: [{'cwe_Name': 'C', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'C++', 'cwe_Prevalence': 'Undetermined'}]
范围 | 影响 | 注释 |
---|---|---|
['Availability', 'Integrity'] | Unexpected State | The assignment will not take place, which should cause obvious program execution problems. |
策略:
Many IDEs and static analysis products will detect this problem.
The following example demonstrates the weakness.
bad Java
The following C/C++ example shows a simple implementation of a stack that includes methods for adding and removing integer values from the stack. The example uses pointers to add and remove integer values to the stack array variable.
bad C
The push method includes an expression to assign the integer value to the location in the stack pointed to by the pointer variable.
However, this expression uses the comparison operator "==" rather than the assignment operator "=". The result of using the comparison operator instead of the assignment operator causes erroneous values to be entered into the stack and can cause unexpected results.
映射的分类名 | ImNode ID | Fit | Mapped Node Name |
---|---|---|---|
CLASP | Comparing instead of assigning | ||
Software Fault Patterns | SFP2 | Unused Entities |