对零进行无符号比较¶
ID: cpp/unsigned-comparison-zero
Kind: problem
Security severity:
Severity: warning
Precision: very-high
Tags:
- maintainability
- readability
Query suites:
- cpp-security-and-quality.qls
此规则查找 x >= 0
形式的表达式,其中 x
是无符号值。这种比较毫无意义,因为它始终返回 1
。
建议¶
检查表达式以查看是否需要不同的语义。
示例¶
typedef long long LONGLONG;
int f(unsigned int u, LONGLONG l) {
if(u > 0 || l >=0) //correct: unsigned value is check for > 0
return 23;
return u >= 0; //wrong: unsigned values are always greater than or equal to 0
}