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

admin 2021年12月16日16:31:30评论92 views字数 2575阅读8分35秒阅读模式

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

Assigning instead of Comparing

结构: Simple

Abstraction: Variant

状态: Draft

被利用可能性: Low

基本描述

The code uses an operator for assignment when the intention was to perform a comparison.

扩展描述

In many languages the compare statement is very close in appearance to the assignment statement and are often confused. This bug is generally the result of a typo and usually causes obvious problems with program execution. If the comparison is in an if statement, the if statement will usually evaluate the value of the right-hand side of the predicate.

相关缺陷

  • 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

  • cwe_Nature: CanPrecede cwe_CWE_ID: 697 cwe_View_ID: 1000

适用平台

Language: [{'cwe_Name': 'C', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'C++', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'Java', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'C#', 'cwe_Prevalence': 'Undetermined'}]

常见的影响

范围 影响 注释
Other Alter Execution Logic

可能的缓解方案

Testing

策略:

Many IDEs and static analysis products will detect this problem.

Implementation

策略:

Place constants on the left. If one attempts to assign a constant with a variable, the compiler will of course produce an error.

示例代码

The following C/C++ and C# examples attempt to validate an int input parameter against the integer value 100.

bad C

int isValid(int value) {

if (value=100) {

printf("Value is validn");
return(1);

}
printf("Value is not validn");
return(0);

}

bad C#

bool isValid(int value) {

if (value=100) {

Console.WriteLine("Value is valid.");
return true;

}
Console.WriteLine("Value is not valid.");
return false;

}

However, the expression to be evaluated in the if statement uses the assignment operator "=" rather than the comparison operator "==". The result of using the assignment operator instead of the comparison operator causes the int variable to be reassigned locally and the expression in the if statement will always evaluate to the value on the right hand side of the expression. This will result in the input value not being properly validated, which can cause unexpected results.

In this example, we show how assigning instead of comparing can impact code when values are being passed by reference instead of by value. Consider a scenario in which a string is being processed from user input. Assume the string has already been formatted such that different user inputs are concatenated with the colon character. When the processString function is called, the test for the colon character will result in an insertion of the colon character instead, adding new input separators. Since the string was passed by reference, the data sentinels will be inserted in the original string (CWE-464), and further processing of the inputs will be altered, possibly malformed..

bad C

void processString (char *str) {

int i;

for(i=0; iif (isalnum(str[i])){

processChar(str[i]);

}
else if (str[i] = ':') {

movingToNewInput();}

}

}

}文章来源于互联网:scap中文网

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2021年12月16日16:31:30
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   CWE-481 错误将比较符号写成赋值符号https://cn-sec.com/archives/612989.html

发表评论

匿名网友 填写信息