The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. However, using the sizeof operator to determine the size of arrays is error prone.
Non-Compliant Code Example
Compliant Solution
Risk Assessment
Incorrectly using the sizeof operator to determine the size of an array can result in a buffer overflow, allowing the execution of arbitrary code.
Recommendation |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
|---|---|---|---|---|---|
ARR00-A |
3 (high) |
2 (probable) |
3 (low) |
P18 |
L1 |
Automated Detection
The LDRA tool suite V 7.6.0 is able to detect violations of this recommendation.
The tool Compass Rose can detect violations of the recommendation, but it cannot distinguish between incomplete array declarations and pointer declarations.
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
[[ISO/IEC 9899-1999]] Section 6.7.5.2, "Array declarators"
[[Drepper 06]] Section 2.1.1, "Respecting Memory Bounds"
06. Arrays (ARR) 06. Arrays (ARR) ARR30-C. Guarantee that array indices are within the valid range