CWE Rule 467
Description
Rule Description
The code calls sizeof() on a malloced pointer type, which always returns the wordsize/8. This can produce an unexpected result if the programmer intended to determine how much memory has been allocated.
Polyspace Implementation
The rule checker checks for these issues:
Possible misuse of sizeof
Wrong type used in sizeof
Examples
Possible misuse of sizeof
This issue occurs
when Polyspace®
Bug Finder™ detects possibly unintended results from
the use of sizeof
operator. For instance:
You use the
sizeof
operator on an array parameter name, expecting the array size. However, the array parameter name by itself is a pointer. Thesizeof
operator returns the size of that pointer.You use the
sizeof
operator on an array element, expecting the array size. However, the operator returns the size of the array element.The size argument of certain functions such as
strncmp
orwcsncpy
is incorrect because you used thesizeof
operator earlier with possibly incorrect expectations. For instance:In a function call
strncmp(string1, string2, num)
,num
is obtained from an incorrect use of thesizeof
operator on a pointer.In a function call
wcsncpy(destination, source, num)
,num
is the not the number of wide characters but a size in bytes obtained by using thesizeof
operator. For instance, you usewcsncpy(destination, source, sizeof(destination) - 1)
instead ofwcsncpy(destination, source, (sizeof(desintation)/sizeof(wchar_t)) - 1)
.
Incorrect use of the sizeof
operator can
cause the following issues:
If you expect the
sizeof
operator to return array size and use the return value to constrain a loop, the number of loop runs are smaller than what you expect.If you use the return value of
sizeof
operator to allocate a buffer, the buffer size is smaller than what you require. Insufficient buffer can lead to resultant weaknesses such as buffer overflows.If you use the return value of
sizeof
operator incorrectly in a function call, the function does not behave as you expect.
Possible fixes are:
Do not use the
sizeof
operator on an array parameter name or array element to determine array size.The best practice is to pass the array size as a separate function parameter and use that parameter in the function body.
Use the
sizeof
operator carefully to determine the number argument of functions such asstrncmp
orwcsncpy
. For instance, for wide string functions such aswcsncpy
, use the number of wide characters as argument instead of the number of bytes.
#define MAX_SIZE 1024 void func(int a[MAX_SIZE]) { int i; for (i = 0; i < sizeof(a)/sizeof(int); i++) //Noncompliant { a[i] = i + 1; } }
In this example, sizeof(a)
returns the size
of the pointer a
and not the array size.
One possible correction is to use another means to determine the array size.
#define MAX_SIZE 1024 void func(int a[MAX_SIZE]) { int i; for (i = 0; i < MAX_SIZE; i++) { a[i] = i + 1; } }
Wrong type used in sizeof
This issue occurs when both of the following conditions hold:
You assign the address of a block of memory to a pointer, or transfer data between two blocks of memory. The assignment or copy uses the
sizeof
operator.For instance, you initialize a pointer using
malloc(sizeof(
or copy data between two addresses usingtype
))memcpy(
.destination_ptr
,source_ptr
, sizeof(type
))You use an incorrect type as argument of the
sizeof
operator. For instance:You might be using the pointer type instead of the type that the pointer points to. For example, to initialize a
pointer, you might be usingtype
*malloc(sizeof(
instead oftype
*))malloc(sizeof(
.type
))You might be using a completely unrelated type as
sizeof
argument. For example, to initialize a
pointer, you might be usingtype
*malloc(sizeof(
.anotherType
))
Irrespective of what type
stands
for, the expression sizeof(
always
returns a fixed size. The size returned is the pointer size on your
platform in bytes. The appearance of type
*)sizeof(
often
indicates an unintended usage. The error can cause allocation of a
memory block that is much smaller than what you need and lead to weaknesses
such as buffer overflows.type*
)
For instance, assume that structType
is a
structure with ten int
variables. If you initialize
a structType*
pointer using malloc(sizeof(structType*))
on
a 32-bit platform, the pointer is assigned a memory block of four
bytes. However, to be allocated completely for one structType
variable,
the structType*
pointer must point to a memory
block of sizeof(structType) = 10 * sizeof(int)
bytes.
The required size is much greater than the actual allocated size of
four bytes.
To initialize a
pointer,
replace type
*sizeof(
in
your pointer initialization expression with type
*)sizeof(
.type
)
#include <stdlib.h> void test_case_1(void) { char* str; str = (char*)malloc(sizeof(char*) * 5); //Noncompliant free(str); }
In this example, memory is allocated for the character pointer str
using
a malloc
of five char pointers. However, str
is
a pointer to a character, not a pointer to a character pointer. Therefore
the sizeof
argument, char*
,
is incorrect.
sizeof
ArgumentOne possible correction is to match the argument
to the pointer type. In this example, str
is a
character pointer, therefore the argument must also be a character.
#include <stdlib.h> void test_case_1(void) { char* str; str = (char*)malloc(sizeof(char) * 5); free(str); }
Check Information
Category: Pointer Issues |
Version History
Introduced in R2023a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)