sizeof operator yields the size (in bytes) of its operand, which can be an expression or the parenthesized name of a type. However, using the
sizeof operator to determine the size of arrays is error prone.
sizeof operator is often used in determining how much memory to allocate via
malloc(). However using an incorrect size is a violation of MEM35-C. Allocate sufficient memory for an object.
Noncompliant Code Example
In this noncompliant code example, the function
clear() zeros the elements in an array. The function has one parameter declared as
int array and is passed a static array consisting of 12
int as the argument. The function
clear() uses the idiom
sizeof(array) / sizeof(array) to determine the number of elements in the array. However,
array has a pointer type because it is a parameter. As a result,
sizeof(array) is equal to the
sizeof(int *). For example, on an architecture (such as IA-32) where the
sizeof(int) == 4 and the
sizeof(int *) == 4, the expression
sizeof(array) / sizeof(array) evaluates to 1, regardless of the length of the array passed, leaving the rest of the array unaffected.
Footnote 103 in subclause 126.96.36.199 of the C Standard [ISO/IEC 9899:2011] applies to all array parameters:
When applied to a parameter declared to have array or function type, the
sizeofoperator yields the size of the adjusted (pointer) type.
In this compliant solution, the size of the array is determined inside the block in which it is declared and passed as an argument to the function:
sizeof(array) / sizeof(array) idiom will succeed provided the original definition of
array is visible.
Noncompliant Code Example
In this noncompliant code example,
sizeof(a) does not equal
100 * sizeof(int), because the
sizeof operator, when applied to a parameter declared to have array type, yields the size of the adjusted (pointer) type even if the parameter declaration specifies a length:
In this compliant solution, the size is specified using the expression
len * sizeof(int):
Incorrectly using the
sizeof operator to determine the size of an array can result in a buffer overflow, allowing the execution of arbitrary code.
Can detect violations of the recommendation but cannot distinguish between incomplete array declarations and pointer declarations
|LDRA tool suite|
|Polyspace Bug Finder||R2016a||Possible misuse of sizeof|
|PVS-Studio||6.22||V511, V512, V514, V568, V579, V604, V697|
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Key here (explains table format and definitions)
|CERT C||CTR01-CPP. Do not apply the sizeof operator to a pointer when taking the size of an array||Prior to 2018-01-12: CERT: Unspecified Relationship|
|CWE 2.11||CWE-467, Use of sizeof() on a pointer type||Prior to 2018-01-12: CERT:|
|ISO/IEC TS 17961||Taking the size of a pointer to determine the size of the pointed-to type [sizeofptr]||Prior to 2018-01-12: CERT: Unspecified Relationship|
|MITRE CWE||CWE-569||Prior to 2018-01-12:|
|MITRE CWE||CWE-783||Prior to 2018-01-12:|
|[Drepper 2006]||Section 2.1.1, "Respecting Memory Bounds"|
|[ISO/IEC 9899:2011]||Subclause 188.8.131.52, "The |