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 C99. Counter intuitivelythe C Standard. Counterintuitively, unspecified behavior in behavior is where for which the standard provides two or more possibilities and imposes no further requirements on which is chosen in any instance. Examples of situations where the 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 Standard, subclause 6.5 [ISO/IEC 9899:2011],

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

Following are specific examples of situations in which the order of evaluation of subexpressions or the order in which side effects take place includeis unspecified:

  • the The order in which the arguments to a function are evaluated (C99C Standard, Section subclause 6.5.2.2, "Function callsCalls").
  • the The order of evaluation of the operands in an assignment statement (C99C Standard, Section subclause 6.5.16, "Assignment operatorsOperators").
  • the 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 (C99C Standard, Section subclause 6.7.89, "Initialization").

Unspecified behavior is generally a portability issue, as 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 orderings, may in fact be executed in a different order.

According to C99, Section 6.5, "Expressions":

Except as specified later (for the function-call (), &&, ||, ?:, and comma operators), the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.

This recommendation is related to EXP30-C. Do not depend on the order of evaluation between sequence points, but for side effects, but it focuses on behavior that is non-portable nonportable or potentially confusing.

...

Noncompliant Code Example

The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is are unspecified, but there is a sequence point before the actual call. For example, in the function call:

Code Block

(*pf[f1()]) (f2(), f3() + f4())

...

the functions {{f1()}}, {{f2()}}, {{f3()}}, and {{f4()}} may be called in any order. All side effects have to be completed before the function pointed to by {{pf\[f1()\]}} is called.

Consequently, the result of this non-compliant noncompliant code example depends upon on unspecified behavior:

Code Block
bgColor#FFcccc
langc
#include <stdio.h>

int g;

int f(int i) {
  g = i;
  return i;
}

int main(void) {
  int x = f(1) + f(2);
 /* Line B */  printf("g = %d\n", g);
  /* ... */
  return 0;
}

This code may result in g being assigned the value 1, or equally likely, being assigned the value 2.

Compliant Solution

This compliant solution is independent of the order of evaluation of the operands and can only be interpreted in only one way.:

Code Block
bgColor#ccccff
langc
#include <stdio.h>

int g;

int f(int i) {
  g = i;
  return i;
}

int main(void) {
  int x = f(1); 
  x += f(2);
  printf("g = %d\n", g);
  /* ... */
  return 0;
}

This code always results in g being assigned the value 2.

Exceptions

EXP10-C-EX1: The && operator guarantees left-to-right evaluation; there is a sequence point after the evaluation of the first operand.EXP10-EX2: The || operator guarantees and || operators guarantee left-to-right evaluation; there is a sequence point after the evaluation of the first operand.

EXP10-C-EX3EX2: The first operand of a condition expression is evaluated; there is a sequence point after its evaluation. The second operand is evaluated only if the first compares unequal to 0; the third operand is evaluated only if the first compares equal to 0.

EXP10-EX4: The left operand of a comma operator is evaluated followed by the right operand. There is a sequence point in between.EXP10-EX5C-EX3: There is a sequence point before function calls, meaning that the function designator, the actual arguments, and subexpressions within the actual arguments are evaluated before the function is invoked.

EXP10-C-EX4: The left operand of a comma operator is evaluated before the right operand is evaluated. There is a sequence point in between.

Note that whereas commas serve to delimit multiple arguments in a function call, these commas are not considered comma operators. Multiple arguments of a function call may be evaluated in any order, with no sequence points between each other.

Risk Assessment

Rule

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

EXP10-

A

2 (medium)

2 (probable)

2 (medium)

P8

L2

C

Medium

Probable

Medium

P8

L2

Automated Detection

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V
evaluation-order
multiple-volatile-accesses
Partially checked
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC-EXP10Fully implemented
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V

LANG.STRUCT.SE.IOE

Indeterminate Order of Evaluation
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

Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C0400, C0401, C0402, C0403, C0404, C0405, C3226, C3326


LDRA tool suite
Include Page
LDRA_V
LDRA_V
35 D, 72 D, 1 Q, 134 S

Fully implemented

Parasoft C/C++test
Include Page
Parasoft_V
Parasoft_V

CERT_C-EXP10-a
CERT_C-EXP10-b
CERT_C-EXP10-c
CERT_C-EXP10-d

The value of an expression shall be the same under any order of evaluation that the standard permits
Don't write code that depends on the order of evaluation of function arguments
Don't write code that depends on the order of evaluation of function designator and function arguments
Don't write code that depends on the order of evaluation of expression that involves a function call

PC-lint Plus

Include Page
PC-lint Plus_V
PC-lint Plus_V

564, 931

Partially supported

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rec. EXP10-C

Checks for situations where expression value depends on order of evaluation or side effects (rec. fully covered)

PVS-Studio

Include Page
PVS-Studio_V
PVS-Studio_V

V521, V681
RuleChecker
Include Page
RuleChecker_V
RuleChecker_V
evaluation-order
multiple-volatile-accesses
Partially checked

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.

Related Vulnerabilities

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

References

Wiki Markup
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.5, "Expressions," Section 6.5.16, "Assignment operators," Section 6.5.2.2, "Function calls," and Section 6.7.8, "Initialization"
\[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] "JCW Operator precedence/Order of Evaluation" and "SAM Side-effects and order of evaluation"
\[[MISRA 04|AA. C References#MISRA 04]\] Rule 12.2

Related Guidelines

SEI CERT C++ Coding StandardEXP50-CPP. Do not depend on the order of evaluation for side effects
ISO/IEC TR 24772:2013Operator Precedence/Order of Evaluation [JCW]
Side-effects and Order of Evaluation [SAM]
MISRA C:2012Rule 13.5 (required)

Bibliography

[ISO/IEC 9899:2011]Subclause 6.5, "Expressions"


...

Image Added Image Added Image AddedDCL13-A. Function arguments that are pointers to values not changed by the function should be declared const      03. Expressions (EXP)       EXP31-C. Do not modify constant values