...
This code always results in g
being assigned the value 2
.
Exceptions
EXP10-C-EX1: The &&
and ||
operators guarantee left-to-right evaluation; there is a sequence point after the evaluation of the first operand.
EXP10-C-EX2: The first operand of a condition expression is evaluated; there is a sequence point after its evaluation. The second operand is evaluated only if the first compares unequal to 0; the third operand is evaluated only if the first compares equal to 0.
EXP10-C-EX3: There is a sequence point before function calls, meaning that the function designator, the actual arguments, and subexpressions within the actual arguments are evaluated before the function is invoked.
EXP10-C-EX4: The left operand of a comma operator is evaluated before the right operand is evaluated. There is a sequence point in between.
Note that whereas commas serve to delimit multiple arguments in a function call, these commas are not considered comma operators. Multiple arguments of a function call may be evaluated in any order, with no sequence points between each other.
Risk Assessment
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
EXP10-C | Medium | Probable | Medium | P8 | L2 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Astrée |
| evaluation-order multiple-volatile-accesses | Partially checked | ||||||
Axivion Bauhaus Suite |
| CertC-EXP10 | Fully implemented | ||||||
CodeSonar |
| LANG.STRUCT.SE.IOE | Indeterminate Order of Evaluation | ||||||
Compass/ROSE |
Could detect violations of this recommendation by searching for the following pattern:
| |||||||||
| EVALUATION_ORDER | Can detect the specific instance where a statement contains multiple side effects on the same value with an undefined evaluation order because the statement may behave differently with different compiler flags or different compilers or platforms | |||||||
Helix QAC |
| C0400, C0401, C0402, C0403, C0404, C0405, C3226, C3326 | |||||||
LDRA tool suite |
| 35 D, 72 D, 1 Q, 134 S | Fully implemented | ||||||
Parasoft C/C++test |
| CERT_C-EXP10-a | The value of an expression shall be the same under any order of evaluation that the standard permits | |||||||
PC-lint Plus |
| 564, 931 | Partially supported | ||||||
Polyspace Bug Finder |
| CERT C: Rec. EXP10-C | Checks for situations where expression value depends on order of evaluation or side effects (rec. fully covered) | ||||||
PVS-Studio |
| V521, V681 | |||||||
RuleChecker |
| evaluation-order multiple-volatile-accesses | Partially checked |
A programmer could also violate the recommendation using dynamic memory passed to both functions, but that would be extremely difficult to detect using static analysis.
...
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
SEI CERT C++ Coding Standard | EXP50-CPP. Do not depend on the order of evaluation for side effects |
ISO/IEC TR 24772:2013 | Operator Precedence/Order of Evaluation [JCW] Side-effects and Order of Evaluation [SAM] |
MISRA C:2012 | Rule 13.5 (required) |
Bibliography
[ISO/IEC 9899:2011] | Subclause 6.5, "Expressions" |
...
...