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.

The C standardStandard, Section 6.5 [ISO/IEC 9899:2011], states:

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 in Annex J of the C Standard.)

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

The following sequence points are defined in Annex C of the C standard Standard [ISO/IEC 9899:2011]:

  • Between the evaluations of the function designator and actual arguments in a function call and the actual call.
  • Between the evaluations of the first and second operands of the following operators:
    • logical AND: &&
    • logical OR: ||
    • comma: ,
  • Between the evaluations of the first operand of the conditional ?: operator and whichever of the second and third operands is evaluated.
  • The end of a full declarator.
  • Between the evaluation of a full expression and the next full expression to be evaluated. The following are full expressions:
    • an initializer that is not part of a compound literal
    • the expression in an expression statement
    • the controlling expression of a selection statement (if or switch)
    • the controlling expression of a while or do statement
    • each of the (optional) expressions of a for statement
    • the (optional) expression in a return statement
  • Immediately before a library function returns.
  • After the actions associated with each formatted input/output function conversion specifier.
  • Immediately before and immediately after each call to a comparison function, and also between any call to a comparison function and any movement of the objects passed as arguments to that call.

...

have well-defined behavior, and statements like the following do not:

Code Block
bgColor#FFcccc
langc
/* i is modified twice between sequence points */
i = ++i + 1;  

/* i is read other than to determine the value to be stored */
a[i++] = i;   

do not.

Noncompliant Code Example

...

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

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.

PRQA QA-C
Include Page
PRQA_V
PRQA_V

0400 [U]
0401 [U]
0402 [U]
0403 [U]

Fully implemented.

Related Vulnerabilities

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

Related Guidelines

...

...

...

EXP05-J. Do not write more than once to the same variable within an expression
ISO/IEC TR 24772JCW Operator precedence/order of evaluation" and "SAM Side-effects and order of evaluation
MISRA-CRule 12.1

Bibliography

...

]Section 5.1.2.3, "Program

...

Execution,"

...

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

MISRA Rule 12.1

Bibliography

section 6.5, "Expressions," and Annex C, "Sequence

...

Points"
[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] 

...