Versions Compared

Key

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

The order of evaluation of subexpressions and the order in which side effects take place are frequently defined as unspecified behavior by the C standardStandard. Counterintuitively, unspecified behavior is where the standard provides two or more possibilities and imposes no further requirements on which is chosen in any instance. Consequently, unspecified behavior can be a portability issue because different implementations can make different choices. If dynamic scheduling is used, however, there may not be a fixed-code execution sequence over the life of a process. Operations that can be executed in different sequences may in fact be executed in a different order.

According to the C standardStandard, Section 6.5 [ISO/IEC 9899:2011],

Except as specified later, side effects and value computations of subexpressions are unsequenced.

...

  • The order in which the arguments to a function are evaluated (C11C Standard, Section 6.5.2.2, "Function callsCalls")
  • The order of evaluation of the operands in an assignment statement (C11C Standard, Section 6.5.16, "Assignment operatorsOperators")
  • The order in which any side effects occur among the initialization list expressions is unspecified. In particular, the evaluation order need not be the same as the order of subobject initialization (C11C Standard, Section 6.7.9, "Initialization")

...

Tool

Version

Checker

Description

Compass/ROSE

 

 

Could detect violations of this recommendation by searching for the following pattern:

  • Any expression that calls two functions between the same sequence points
  • Those two functions both modify the value of a static variable
  • That static variable's value is referenced by code following the expression

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 the statement may behave differently with different compiler flags or different compilers or platforms.

LDRA tool suite

Include Page
LDRA_V
LDRA_V

35 D
72 D
74 D
1 Q
134 S

Fully implemented.

PRQA QA-C
Include Page
PRQA_V
PRQA_V
3226Partially implemented.

A programmer could also violate the recommendation using dynamic memory passed to both functions, but that would be extremely difficult to detect using static analysis.

...

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

...

EXP10-CPP. Do not depend on the order of evaluation of subexpressions or the order in which side effects take place
ISO/IEC TR 24772Operator precedence/order of evaluation [JCW] and Side-effects and order of evaluation [SAM]
MISRA-CRule 12.2

Bibliography

...

]Section 6.5, "Expressions," Section 6.5.16, "Assignment

...

Operators," Section 6.5.2.2, "Function

...

Calls," and Section 6.7.9, "Initialization"

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

MISRA Rule 12.2

...

 

...