Versions Compared

Key

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

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

Non-Compliant Code Example

In this example, the variable a will still have a value 14 after b has been initialized. following program compiles cleanly under Microsoft Visual Studio 2005 Version 8.0, with the /W4 option:

Code Block
int main(void) {
  int a = 14;
  int b = sizeof( a++ );
  printf("a, b = %d, %d.\n", a, b);  /* prints a, b = 14, 4. */
  return 0;
}

The indicated expression will be discarded; consequently, any expression a++ is not evaluated. Consequently, side effects in that the expression will are not be executed.In the example, the variable a will still have a value 14 after b has
been initialized.Whether or not a size expression is evaluated when it is part of the operand of a
sizeof operator and changing the value of the size expression would not affect the
result of the operator (6.7.5.2).

Implementation Specific Details

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

Priority: P4 Level: L3

If the object really is constant, the compiler may have put it in ROM or write-protected memory. Trying to modify such an object may lead to a program crash. This could allow an attacker to mount a denial-of-service attack.

Component

Value

Severity

1 (low)

Likelihood

2 (probable)

Remediation cost

2 (medium)

References