
...
The call to func()
in this noncompliant code example has undefined behavior 34 because there is no sequence point between the argument expressions:
...
The first (left) argument expression reads the value of i
(to determine the value to be stored) and then modifies i
. The second (right) argument expression reads the value of i
between the same pair of sequence points as the first argument, but not to determine the value to be stored in i
. This additional attempt to read the value of i
has undefined has undefined behavior 34.
Compliant Solution
This compliant solution is appropriate when the programmer intends for both arguments to func()
to be equivalent:
...
Tool | Version | Checker | Description | |||||||
---|---|---|---|---|---|---|---|---|---|---|
Astrée |
| evaluation-order multiple-volatile-accesses | Fully checked | |||||||
Axivion Bauhaus Suite |
| CertC-EXP30 | ||||||||
Clang |
| -Wunsequenced | Detects simple violations of this rule, but does not diagnose unsequenced function call arguments. | |||||||
CodeSonar |
| LANG.STRUCT.SE.DEC | Side Effects in Expression with Decrement | |||||||
Compass/ROSE | Can detect simple violations of this rule. It needs to examine each expression and make sure that no variable is modified twice in the expression. It also must check that no variable is modified once, then read elsewhere, with the single exception that a variable may appear on both the left and right of an assignment operator | |||||||||
| EVALUATION_ORDER | Can detect the specific instance where a statement contains multiple side effects on the same value with an undefined evaluation order because, with different compiler flags or different compilers or platforms, the statement may behave differently | ||||||||
Cppcheck |
| unknownEvaluationOrder | Partially implemented | |||||||
Cppcheck Premium |
| unknownEvaluationOrderPartially implemented | ||||||||
| CC2.EXP30 | Fully implemented | ||||||||
GCC |
| Can detect violations of this rule when the | ||||||||
Helix QAC |
| C0400, C0401, C0402, C0403, C0404, C0405 | Fully implemented | |||||||
Klocwork |
| PORTING.VAR.EFFECTS | Fully implemented | |||||||
LDRA tool suite |
| 35 D, 1 Q, 9 S, 30 S, 134 S | Partially implemented | |||||||
Parasoft C/C++test |
| CERT_C-EXP30-a | The value of an expression shall be the same under any order of evaluation that the standard permits | |||||||
PC-lint Plus |
| 564 | Partially supported | |||||||
Polyspace Bug Finder |
| CERT C: Rule EXP30-C | Checks for situations when expression value depends on order of evaluation or of side effects (rule partially covered) | |||||||
PVS-Studio |
| V532, V567 | ||||||||
RuleChecker |
| evaluation-order multiple-volatile-accesses | Fully checked | |||||||
Splint |
| |||||||||
SonarQube C/C++ Plugin |
| IncAndDecMixedWithOtherOperators | ||||||||
TrustInSoft Analyzer |
| separated | Exhaustively verified (see one compliant and one non-compliant example). |
...