Versions Compared

Key

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

Evaluation of an expression may produce side effects. At specific points during execution, known as sequence points, all side effects of previous evaluations have completed, and no side effects of subsequent evaluations have yet taken place.

...

If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.

(See also undefined behavior 35  of Annex J.)

This requirement must be met for each allowable ordering of the subexpressions of a full expression; otherwise, the behavior is undefined.

...

Code Block
bgColorFFcccc
langc
func(i++, i);

The call to func() has undefined behavior because there are no sequence points 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 behavior.

...

Tool

Version

Checker

Description

Splint

Include Page
Splint_V
Splint_V

 

 

GCC

Include Page
GCC_V
GCC_V

 

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

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 Prevent

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.

LDRA tool suite

Include Page
LDRA_V
LDRA_V

35 D
1 Q
9 S
30 S
134 S

Fully implemented.

...

ISO/IEC 9899:2011 Section 5.1.2.3, "Program execution," Section 6.5, "Expressions," and Annex C, "Sequence points"

ISO/IEC TR 24772 "JCW Operator precedence/order of evaluation" and "SAM Side-effects and order of evaluation"

MISRA Rule 12.1

Bibliography

[Summit 2005] Questions 3.1, 3.2, 3.3, 3.3b, 3.7, 3.8, 3.9, 3.10a, 3.10b, and 3.11
[Saks 2007]

...