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.
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 expression a++
is not evaluated. Consequently, side effects in the expression are not executed.
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
- ISO/IEC 9899-1999 Section 6.5.3.4 The sizeof operator