CERT-EXP30-C_a
Synopsis
Do not depend on the order of evaluation for side effects.
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
Evaluation of an expression may produce side effects. At specific points during execution, known as sequence points, all side effects of previous evaluations are complete, and no side effects of subsequent evaluations have yet taken place. Do not depend on the order of evaluation for side effects unless there is an intervening sequence point. This check is identical to MISRAC++2008-5-0-1_a, MISRAC2004-12.2_a, MISRAC2012-Rule-1.3_i, MISRAC2012-Rule-13.2_a, SPC-order.
Coding standards
- CERT EXP30-C
Do not depend on order of evaluation between sequence points
- MISRA C:2004 12.2
(Required) The value of an expression shall be the same under any order of evaluation that the standard permits.
- MISRA C:2012 Rule-1.3
(Required) There shall be no occurrence of undefined or critical unspecified behaviour
- MISRA C:2012 Rule-13.2
(Required) The value of an expression and its persistent side effects shall be the same under all permitted evaluation orders
- MISRA C++ 2008 5-0-1
(Required) The value of an expression shall be the same under any order of evaluation that the standard permits.
Code examples
The following code example fails the check and will give a warning:
void example(int i, int *b) {
int a = i + b[++i];
}
The following code example passes the check and will not give a warning about this issue:
void example(int i, int *b) {
{
int a;
++i;
a = i + b[i];
}
{
int a = i + b[i + 1];
++i;
}
}