CertC-EXP46¶
Do not use a bitwise operator with a Boolean-like operand
Required inputs: IR
Mixing bitwise and relational operators in the same full expression can be a
sign of a logic error in the expression where a logical operator is usually the
intended operator. Do not use the bitwise AND (
&), bitwise OR (
|), or bitwise XOR (
^) operators with an operand of
type
_Bool, or the result of a relational-expression or equality-expression. If the bitwise operator
is intended, it should be indicated with use of a parenthesized expression.
Noncompliant Code Example
In this noncompliant code example, a bitwise
& operator is used with the results of an
equality-expression:
if (!(getuid() & geteuid() == 0)) {
/* ... */
}
Compliant Solution
This compliant solution uses the
&& operator for the logical operation within the
conditional expression:
if (!(getuid() && geteuid() == 0)) {
/* ... */
}
Risk Assessment
| Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
| EXP46-C | Low | Likely | Low | P9 | L2 |
Related Guidelines
| Taxonomy | Taxonomy item | Relationship |
|---|---|---|
| ISO/IEC TR 24772:2013 | Likely Incorrect Expression [KOA] | Prior to 2018-01-12: CERT: Unspecified Relationship |
| CWE 2.11 | CWE-480, Use of incorrect operator | 2017-07-05: CERT: Rule subset of CWE |
| CWE 2.11 | CWE-569 | 2017-07-06: CERT: Rule subset of CWE |
Bibliography
| [ Hatton 1995] | Section 2.7.2, "Errors of Omission and Addition" |
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
bool_operand_in_bad_operator |
Use of boolean operand in ‘{}’ operator |
None |
False |
Options
This rule shares the following common options: exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
This rule has no individual options.