Versions Compared

Key

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

Some operators do not evaluate their operands beyond the type information they provide. When using one of these operators, do not pass an operand which would otherwise yield a side effect, as the side effect will not be generated.

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. When the type of the operand is a variable length array type (VLA) the expression is evaluated; otherwise, the operand is not evaluated.

When part of the operand of the sizeof operator is a VLA type and when changing the value of the VLA's size expression would not affect the result of the operator, it is unspecified whether or not the size expression is evaluated. See unspecified behavior 22 in Annex J of the C Standard.

The operands passed to _Generic and _Alignof are never evaluated.

Providing an expression that appears to produce side effects may be misleading to programmers who are not aware that these expressions are not evaluated, and in the non-VLA case and have case of a VLA used in sizeof, have unspecified results otherwise. As a result, programmers may make invalid assumptions about program state, leading to errors and possible software vulnerabilities.

Noncompliant Code Example (sizeof)

In this noncompliant code example, the expression a++ is not evaluated, and the side effects in the expression are not executed:

Code Block
bgColor#FFcccc
langc
void func(void) {
  int a = 14;
  int b = sizeof(a++);

}

Consequently, the value of a after b has been initialized is 14.

Implementation-Specific Details

This example compiles cleanly under Microsoft Visual Studio 2005, version 8.0, with the /W4 option.

...

Compliant Solution (sizeof)

In this compliant solution, the variable a is incremented:

Code Block
bgColor#ccccff
langc
void func(void) {
  int a = 14;
  int b = sizeof(a);
a  ++a;

}

Anchor
ncce_vla
ncce_vla

Noncompliant Code Example (sizeof, Variable Length Array)

In the following noncompliant code example, the expression ++n in the initialization expression of a must be evaluated because its value affects the size of the VLA operand of the sizeof operator. However, because the expression ++n % 1 evaluates to 0, regardless of the value of n, its value does not affect the result of the sizeof operator. Consequently, it is unspecified whether or not n is incremented.

Code Block
bgColor#FFcccc
langc
#include <stddef.h>
 
void f(size_t n) {
  /* n must be incremented */ 
  size_t a = sizeof(int [++n]);  
 
       /* n need mustnot be incremented */
  size_t b = sizeof(int [++n % 1 + 1]); /* n need not be incremented */
  
  /* ... */
}

Anchor
cs_vla
cs_vla

Compliant Solution (sizeof, Variable Length Array)

This compliant solution avoids changing the value of the variable n used in the sizeof expression and instead increments it safely outside of it:

Code Block
bgColor#ccccFF
langc
#include <stddef.h>
 
void f(size_t n) {
  size_t a = sizeof(int [n + 1]);
  ++n;

  size_t b = sizeof(int [n % 1 + 1]);
  ++n;
  /* ... */
}

Noncompliant Code Example (_Generic)

This compliant solution attempts to modify a variable's value as part of the _Generic association list. The user may have expected the value of a to be 2 because the type of the variable is int, but because _Generic does not evaluate its operands, the result is undefined behavior.

Code Block
bgColor#FFcccc
langc
#define S(val) _Generic(val, int : val = 2, \
                             short : val = 3, \
                             default : val = 1)
void func(void) {
  int a = 0;
  S(a);
}

Compliant Solution (_Generic)

This compliant solution uses the result of the _Generic operator to perform the side-effect.

Code Block
bgColor#ccccFF
langc
#define S(val) _Generic(val, int : 2, \
                             short : 3, \
                             default : 1)
void func(void) {
  int a = 0;
  a = S(a);
} 

Risk Assessment

If expressions that appear to produce side effects are supplied to the sizeof operatoran operator that does not evaluate its operands, the returned result results may be different than expected. Depending on how this result is used, it can lead to unintended program behavior.

RecommendationRule

Severity

Likelihood

Remediation Cost

Priority

Level

EXP06EXP44-C

Low

Unlikely

Low

P3

L3

Automated Detection

Tool

Version

Checker

Description

Compass/ROSE

 

 

 

ECLAIR

Include Page
ECLAIR_V
ECLAIR_V

CC2.EXP06

Fully implemented

    

LDRA tool suite

Include Page
LDRA_V
LDRA_V

54 S

Fully implemented

PRQA QA-C
Include Page
PRQA_V
PRQA_V
3307Fully implemented

Related Vulnerabilities

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

Related Guidelines

 

...