You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 16 Next »

The order of evaluation of subexpressions, and the order in which side effects take place, are frequently defined as unspecified behavior by C99. Counter intuitively, 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, 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.

Specific examples of situations where the order of evaluation of subexpressions or the order in which side effects take place is unspecified include

  • The order in which the arguments to a function are evaluated (C99, Section 6.5.2.2, "Function calls").
  • The order of evaluation of the operands in an assignment statement (C99, Section 6.5.16, "Assignment operators").
  • 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 (C99, Section 6.7.8, "Initialization").

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

Non-Compliant Code Example

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

For example, in the function call

(*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 the following non-compliant code depends upon unspecified behavior:

int g;

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

int main(void) {
  int x = f(1) + f(2); /* Line B */ 
  /* ... */
  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 one way.

int g;

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

int main(void) {
  int x = f(1); 
  x += f(2);
  /* ... */
  return 0;
}

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

Exceptions

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

EXP10-EX2: 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-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-EX4: The left operand of a comma operator is evaluated followed by the right operand. There is a sequence point in between.

Note that while 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 in between each other.

Risk Assessment

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

EXP10-A

medium

probable

medium

P8

L2

Related Vulnerabilities

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

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]] "JCW Operator precedence/Order of Evaluation" and "SAM Side-effects and order of evaluation"
[[MISRA 04]] Rule 12.2


      03. Expressions (EXP)       EXP11-A. Do not apply operators expecting one type to data of an incompatible type

  • No labels