Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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
bgColor#FFcccc
langc
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

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

Coverity

Include Page
Coverity_V
Coverity_V

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

ECLAIR

Include Page
ECLAIR_V
ECLAIR_V

CC2.EXP30

Fully implemented

GCC

Include Page
GCC_V
GCC_V

 

Can detect violations of this rule when the -Wsequence-point flag is used

LDRA tool suite

Include Page
LDRA_V
LDRA_V

35 D
1 Q

9 S

134 S

Fully implemented

Splint

Include Page
Splint_V
Splint_V

 

 

PRQA QA-C++
Include Page
PRQA QA-C++_V
PRQA QA-C++_V
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."

 

...