界: Code Quality

代码质量不佳会导致不可预测的行为。对于用户来说,通常表现为可用性差。对于攻击者来说,提供了以意外方式对系统施加压力的机会。

Type Mismatch: Negative to Unsigned

Abstract
该函数声明为返回无符号值,但在某些情况下它会尝试返回负值。
Explanation
依赖在带符号和不带符号的数字之间进行隐式转换是很危险的,因为转换的结果可能是一个超出预料的值,并且会违反程序员在程序中所作的其他假设。

示例 1:在此示例中,变量 amount 在返回时可能包含负值。由于函数已声明为返回不带符号的整数,因此 amount 将隐式转换为无符号的值。


unsigned int readdata () {
int amount = 0;
...
if (result == ERROR)
amount = -1;
...
return amount;
}


如果满足Example 1 中的错误条件,则 readdata() 的返回值在使用 32 位整型的系统上将为 4,294,967,295。

在带符号和不带符号的值之间进行转换会引发各种错误,但从安全性角度来说,最常见的是引发 integer overflow 和 buffer overflow 漏洞。
References
[1] Standards Mapping - Common Weakness Enumeration CWE ID 195
[2] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-002824
[3] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C Guidelines 2012 Rule 10.3
[4] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C Guidelines 2023 Rule 7.5, Rule 7.6, Rule 10.3, Rule 21.18
[5] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C++ Guidelines 2008 Rule 5-0-3
[6] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C++ Guidelines 2023 Rule 7.0.5, Rule 7.0.6
[7] Standards Mapping - NIST Special Publication 800-53 Revision 4 SI-16 Memory Protection (P1)
[8] Standards Mapping - NIST Special Publication 800-53 Revision 5 SI-16 Memory Protection
[9] Standards Mapping - Payment Card Industry Data Security Standard Version 3.0 Requirement 6.5.6
[10] Standards Mapping - Payment Card Industry Data Security Standard Version 3.1 Requirement 6.5.6
[11] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2 Requirement 6.5.6
[12] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2.1 Requirement 6.5.6
[13] Standards Mapping - Payment Card Industry Data Security Standard Version 4.0 Requirement 6.2.4
[14] Standards Mapping - Payment Card Industry Data Security Standard Version 4.0.1 Requirement 6.2.4
[15] Standards Mapping - Payment Card Industry Software Security Framework 1.0 Control Objective 4.2 - Critical Asset Protection
[16] Standards Mapping - Payment Card Industry Software Security Framework 1.1 Control Objective 4.2 - Critical Asset Protection
[17] Standards Mapping - Payment Card Industry Software Security Framework 1.2 Control Objective 4.2 - Critical Asset Protection
[18] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP3550 CAT I
[19] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP3550 CAT I
[20] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP3550 CAT I
[21] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP3550 CAT I
[22] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP3550 CAT I
[23] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP3550 CAT I
[24] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP3550 CAT I
[25] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-002590 CAT I
[26] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-002590 CAT I
[27] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-002590 CAT I
[28] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-002590 CAT I
[29] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-002590 CAT I
[30] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-002590 CAT I
[31] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-002590 CAT I
[32] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-002590 CAT I
[33] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-002590 CAT I
[34] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-002590 CAT I
[35] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-002590 CAT I
[36] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-002590 CAT I
[37] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-002590 CAT I
[38] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-002590 CAT I
[39] Standards Mapping - Security Technical Implementation Guide Version 6.1 APSC-DV-002590 CAT I
desc.structural.cpp.type_mismatch_negative_to_unsigned