含糊无符号位域成员¶
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
在 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;