The order in which operands in an expression are evaluated is undefined in C except at the sequence points.
Evaluation of an expression may produce side effects. At specific points in the execution sequence called sequence points, all side effects of previous evaluations have completed, and no side effects of subsequent evaluations have yet taken place.
The following are the sequence points defined by C99:
- the call to a function, after the arguments have been evaluated
- the end of the first operand of the following operators: logical AND &&; logical OR ||; conditional ?; comma ,
- the end of a full declarator: declarators;
- the end of a full expression: an initializer; the expression in an expression statement; the controlling expression of a selection statement (if or switch); the controlling expression of a while or do statement; each of the expressions of a for statement; the expression in a return statement
- immediately before a library function returns (7.1.4)
- after the actions associated with each formatted input/output function conversion specifier
- immediately before and immediately after each call to a comparison function, and also between any call to a comparison function and any movement of the objects passed as arguments to that call
Between the previous and next sequence point an object can only have its stored value modified once by the evaluation of an expression. Additionally, the prior value can be read only to determine the value to be stored.
This rule means that statements such as
i = i + 1;
are allowed, while statements like
i = i++;
are not allowed because they modify the same value twice.
Non-Compliant Code Example
In this example, the order of evaluation of the operands to + is undefined.
a = i + b[++i];
If i
was equal to 0 before the statement, this statement may result in the following outcome:
a = 0 + b[1];
Or it may legally result in the following outcome:
a = 1 + b[1];
As a result, programs cannot safely rely on the order of evaluation of operands between sequence points.
Compliant Solution
These examples are independent of the order of evaluation of the operands and can only be interpreted in one way.
++i; a = i + b[i];
Or alternatively:
a = i + b[i+1]; ++i;
Non-Compliant Code Example
There is no ordering of subexpressions implied by the assignment operator, so the behavior of these statements is undefined.
i = ++i + 1; a[i++] = i;
Compliant Solution
These statements are allowed by the standard.
i = i + 1; a[i] = i;
Non-Compliant Code Example
The order of evaluation of arguments to a function is undefined.
func(i++, i++);
Compliant Solution
This solution is appropriate when the programmer intends for both arguments to func()
to be equivalent.
i++; func(i, i);
This solution is appropriate when the programmer intends for the second argument to be one greater than the first.
j = i; j++; func(i, j);
Risk Assessment
Modifying an object multiple times between sequence points may cause that object to take on an unexpected value. This can lead to unexpected program behavior.
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
EXP30-C |
2 (mediium) |
2 (probable) |
2 (medium) |
P8 |
L2 |
References
[[ISO/IEC 9899-1999]] Section 5.1.2.3, "Program execution"
[[ISO/IEC 9899-1999]] Section 6.5, "Expressions"
[[ISO/IEC 9899-1999]] Annex C, "Sequence points"
[[Summit 05]] Questions 3.1, 3.2, 3.3, 3.3b, 3.7, 3.8, 3.9, 3.10a, 3.10b, 3.11