...
Code Block | ||||
---|---|---|---|---|
| ||||
int a = 2; int b = 2; int c = 2; /* ... */ if (a < b < c) /* Misleading,; likely bug */ /* ... */ if (a == b == c) /* Misleading,; likely bug */ |
The expression a < b < c
evaluates to true rather than, as its author probably intended, to false, and the expression a == b == c
evaluates to false rather than, as its author probably intended, to true.
...
Code Block | ||||
---|---|---|---|---|
| ||||
if ( (a < b) && (b < c) ) /* Clearer, and probably what was intended */
/* ... */
if ( (a == b) && (a == c) ) /* Ditto */
|
...
Incorrect use of relational and equality operators can lead to incorrect control flow.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
EXP13-C |
Low |
Unlikely |
Medium | P2 | L3 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Astrée |
| chained-comparison | Fully checked | ||||||
| CC2.EXP13 | Fully implemented | |||||||
GCC |
|
Option |
3392
3401
4111
4112
4113
Related Guidelines
Helix QAC |
| C3392, C3401, C4111, C4112, C4113 | |||||||
LDRA tool suite |
| 433 S | Fully implemented | ||||||
PC-lint Plus |
| 503, 731 | Fully supported | ||||||
Polyspace Bug Finder |
| CERT C: Rec. EXP13-C | Checks for possibly unintended evaluation of expression because of operator precedence rules (rec. fully covered) | ||||||
PVS-Studio |
| V709 | |||||||
RuleChecker |
| chained-comparison | Fully checked |
Related Guidelines
SEI CERT C++ Coding Standard | VOID |
EXP17-CPP. Treat relational and equality operators as if they were nonassociative |
Bibliography
[ISO/IEC 9899:2011] | Subclause 6.5.8, "Relational Operators" |
...
...