CWE-482 错误将赋值符号写成比较符号

Comparing instead of Assigning

结构: 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.

可能的缓解方案

Testing

策略:

Many IDEs and static analysis products will detect this problem.

示例代码

The following example demonstrates the weakness.

bad Java

void called(int foo) {
foo==1;
if (foo==1) System.out.println("foo\n");
}
int main() {

called(2);
return 0;
}

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

#define SIZE 50
int tos, p1, stack[SIZE];

void push(int i) {
p1++;
if(p1==(tos+SIZE)) {

// Print stack overflow error message and exit
}
p1 == i;
}

int pop(void) {
if(p1==tos) {

// Print stack underflow error message and exit
}
p1--;
return
(p1+1);
}

int main(int argc, char *argv[]) {

// initialize tos and p1 to point to the top of stack
tos = stack;
p1 = stack;
// code to add and remove items from stack
...
return 0;
}

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

引用