The relational and equality operators are left-associative in C. Consequently, C, unlike many other languages, allows chaining of relational and equality operators. Subclause 6.5.8, footnote 107, of the C Standard [ISO/IEC 9899:2011], says:
The expression
a<b<c
is not interpreted as in ordinary mathematics. As the syntax indicates, it means(a<b)<c
; in other words, "ifa
is less thanb
, compare 1 toc
; otherwise, compare 0 toc
."
These operators are left-associative, which means the leftmost comparison is performed first, and the result is compared with the rightmost comparison. This syntax allows a not non-associative as they often are in other languages. This allows a C++ programmer to write an expression (particularly an expression used as a condition) that can be easily misinterpreted.
...
Noncompliant Code Example
Although this noncompliant code example compiles correctly, it is unlikely that it means what the author of the code intended:
Code Block | ||||
---|---|---|---|---|
| ||||
int a = 2; int b = 2; int c = 2; //* ... */ if ( a < b < c ) //* condition #1, misleading, Misleading; likely bug */ //* ... */ if ( a == b == c ) // condition #2, misleading, * Misleading; likely bug */ |
The expression a < b < c
evaluates to true rather than, While the code in the Non-Compliant Code Example compiles correctly, it is unlikely that it means what the author of the code intended. Condition #1 will evaluate to true, rather than false as its author probably intended, and condition #2 will evaluate to false, rather than true , to false, and the expression a == b == c
evaluates to false rather than, as its author probably intended, to true.
Compliant Solution
Treat relational and equality operators as if it were invalid to chain them.:
Code Block | ||||
---|---|---|---|---|
| ||||
if ( (a < b) && (b < c) ) //* clearer,Clearer and probably what was intended */ //* ... */ if ( (a == b) && (a == c) ) /* Ditto */ ditto |
Risk Assessment
Incorrect use of relational and equality operators can lead to incorrect control flow.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|
EXP13- |
1 (low)
1 (unlikely)
C | Low | Unlikely | Medium | P2 | L3 |
Other Languages
...
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Astrée |
| chained-comparison | Fully checked | ||||||
| CC2.EXP13 | Fully implemented | |||||||
GCC |
| Option | |||||||
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" |
...
.EXP08-A. A switch statement should have a default clause unless every enumeration value is tested 03. Expressions (EXP) EXP30-C. Do not cast away const