In C++, modifying an object, calling a library I/O function, accessing a volatile
-qualified value, or calling a function which that performs one of these actions , are ways to modify the state of the execution environment. These actions are called side effects. All relationships between value computations and side effects can be described in terms of sequencing of their evaluations. The C++ Standard, [intro.execution], paragraph 13 [ISO/IEC 14882-2014], establishes three sequencing terms:
Sequenced before is an asymmetric, transitive, pair-wise relation between evaluations executed by a single thread, which induces a partial order among those evaluations. Given any two evaluations A and B, if A is sequenced before B, then the execution of A shall precede the execution of B. If A is not sequenced before B and B is not sequenced before A, then A and B are unsequenced. [Note: The execution of unsequenced evaluations can overlap. — end note] Evaluations A and B are indeterminately sequenced when either A is sequenced before B or B is sequenced before A, but it is unspecified which. [Note: Indeterminately sequenced evaluations cannot overlap, but either could be executed first. — end note]
Paragraph 15 then goes on to state further states (nonnormative text removed for brevity):
...
Note that not all instances of a comma in C++ code denote a usage of the comma operator. For example, the comma between arguments in a function call is NOT not the comma operator.
Noncompliant Code Example
In this noncompliant code example, i
is evaluated more than one once in an unsequenced manner, and so the behavior of the expression is undefined:
...
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
, but not to determine the value to be stored in i
. This additional attempt to read the value of i
has undefined behavior..
Compliant Solution
This compliant solution is appropriate when the programmer intends for both arguments to func()
to be equivalent:
...
Code Block | ||||
---|---|---|---|---|
| ||||
extern void c(int i, int j); int glob; int a() { return glob + 10; } int b() { glob = 42; return glob; } void f() { c(a(), b()); } |
It is unspecified what order The order in which a()
and b()
are called inis unspecified; the only guarantee is that both a()
and b()
will be called before c()
is called. If a()
or b()
rely on shared state when calculating their return value, as they do in this example, the resulting arguments passed to c()
may differ between compilers or architectures.
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
|
| 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 | |||||||
| CC2.EXP30 | Fully implemented | |||||||
|
| Can detect violations of this rule when the | |||||||
| 35 D | Fully implemented | |||||||
|
|
| |||||||
PRQA QA-C++ |
| 3220, 3221, 3222, 3223 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
...
[ISO/IEC 14882-2014] | 1.9, "Program Execution" |
[MISRA 08] | Rule 5-0-1, "The value of an expression shall be the same under any order of evaluation that the standard permits." |
...