CodeQL 文档

含糊无符号位域成员

ID: cpp/ambiguously-signed-bit-field
Kind: problem
Security severity: 
Severity: warning
Precision: high
Tags:
   - reliability
   - readability
   - language-features
   - external/cwe/cwe-190
Query suites:
   - cpp-security-and-quality.qls

单击查看 CodeQL 存储库中的查询

在 C 和旧版本的 C++ 中,普通 char、short、int 或 long 位域的符号性是实现特定的,显式声明其符号性可以消除歧义并确保可移植性。

建议

用显式符号性声明位域的所有成员。

示例

struct {
	int s : 4; //wrong: behavior of bit-field members with implicit signage vary across compilers
	unsigned int : 24; //correct: explicitly unsigned
	signed int : 4; //correct: explicitly signed
} bits;

参考资料

  • AV 规则 154,联合攻击战斗机空中航行器 C++ 编码标准。洛克希德·马丁公司,2005 年。

  • C++ 位域

  • 常见弱点枚举:CWE-190.

  • ©GitHub, Inc.
  • 条款
  • 隐私