CWE Rule 843
Description
Rule Description
The program allocates or initializes a resource such as a pointer, object, or variable using one type, but it later accesses that resource using a type that is incompatible with the original type.
Polyspace Implementation
The rule checker checks for these issues:
Cast to pointer pointing to object of different type
Reading memory reallocated from object of another type without reinitializing first
Examples
Cast to pointer pointing to object of different type
The issue occurs when you perform a cast between a pointer to an object type and a pointer to a different object type.
If a pointer to an object is cast into a pointer to a different object, the resulting pointer can be incorrectly aligned. The incorrect alignment causes undefined behavior.
Even if the conversion produces a pointer that is correctly aligned, the behavior can be undefined if the pointer is used to access an object.
Exception: You can convert a pointer to object type into a pointer to one of these types:
char
signed char
unsigned char
signed char *p1; unsigned int *p2; void foo(void){ p2 = ( unsigned int * ) p1; /* Non-compliant */ }
In this example, p1
can point to a signed char
object. However, p1
is cast to a pointer that points to an object of
wider type, unsigned int
.
extern unsigned int read_value ( void ); extern void display ( unsigned int n ); void foo ( void ){ unsigned int u = read_value ( ); unsigned short *hi_p = ( unsigned short * ) &u; /* Non-compliant */ *hi_p = 0; display ( u ); }
In this example, u
is an unsigned int
variable.
&u
is cast to a pointer that points to an object of narrower
type, unsigned short
.
On a big-endian machine, the statement *hi_p = 0
attempts to clear
the high bits of the memory location that &u
points to. But, from
the result of display(u)
, you might find that the high bits have not
been cleared.
const short *p; const volatile short *q; void foo (void){ q = ( const volatile short * ) p; /* Compliant */ }
In this example, both p
and q
can point to
short
objects. The cast between them adds a
volatile
qualifier only and is therefore compliant.
Reading memory reallocated from object of another type without reinitializing first
This issue occurs when you do the following in sequence:
Reallocate memory to an object with a type that is different from the original allocation.
For instance, in this code snippet, a memory originally allocated to a pointer with type
struct A*
is reallocated to a pointer with typestruct B*
:struct A; struct B; struct A *Aptr = (struct A*) malloc(sizeof(struct A)); struct B *Bptr = (struct B*) realloc(Aptr, sizeof(struct B));
Read from this reallocated memory without reinitializing the memory first.
Read accesses on the pointer to the reallocated memory can happen through pointer dereference or array indexing. Passing the pointer to a function that takes a pointer to a
const
-qualified object as the corresponding parameter also counts as a read access.
Reading from reallocated memory that has not been reinitialized leads to undefined behavior.
Reinitialize memory after reallocation and before the first read access.
The checker considers any write access on the pointer to the reallocated memory as
satisfying the reinitialization requirement (even if the object might only be partially
reinitialized). Write accesses on the pointer to the reallocated memory can happen through
pointer dereference or array indexing. Passing the pointer to a function that takes a
pointer to a non-const
-qualified object as the corresponding parameter
also counts as a write access.
#include<stdlib.h> struct group { char *groupFirst; int groupSize; }; struct groupWithID { int groupID; char *groupFirst; int groupSize; }; char* readName(); int readSize(); void createGroup(int nextAvailableID) { struct group *aGroup; struct groupWithID *aGroupWithID; aGroup = (struct group*) malloc(sizeof(struct group)); if(!aGroup) { /*Handle error*/ } aGroup->groupFirst = readName(); aGroup->groupSize = readSize(); /* Reassign to group with ID */ aGroupWithID = (struct groupWithID*) realloc(aGroup, sizeof(struct groupWithID)); if(!aGroupWithID) { free(aGroup); /*Handle error*/ } if(aGroupWithID -> groupSize > 0) { /* Noncompliant */ /* ... */ } /* ...*/ free(aGroupWithID); }
In this example, the memory allocated to a group*
pointer using the
malloc
function is reallocated to a groupWithID*
pointer using the realloc
function. There is a read access on the
reallocated memory before the memory is reinitialized.
Reinitialize the memory assigned to the groupWithID*
pointer before
the first read access. All bits of the memory can be reinitialized using the
memset
function.
#include<stdlib.h> #include<string.h> struct group { char *groupFirst; int groupSize; }; struct groupWithID { int groupID; char *groupFirst; int groupSize; }; char* readName(); int readSize(); void createGroup(int nextAvailableID) { struct group *aGroup; struct groupWithID *aGroupWithID; aGroup = (struct group*) malloc(sizeof(struct group)); if(!aGroup) { /*Handle error*/ } aGroup->groupFirst = readName(); aGroup->groupSize = readSize(); /* Reassign to group with ID */ aGroupWithID = (struct groupWithID*) realloc(aGroup, sizeof(struct groupWithID)); if(!aGroupWithID) { free(aGroup); /*Handle error*/ } memset(aGroupWithID, 0 , sizeof(struct groupWithID)); /* Reinitialize group */ if(aGroupWithID -> groupSize > 0) { /* ... */ } /* ...*/ free(aGroupWithID); }
Check Information
Category: Type Errors |
Version History
Introduced in R2023aR2024b: Violation reported on conversion of qualified object pointers
Polyspace® does not report a violation if you convert pointers to nonqualified object type into a pointer to one of the following types:
char
signed char
unsigned char
Polyspace reports a violation if you convert pointers to qualified objects even if the
destination type is one of char*
, signed char*
, or
unsigned char*
.
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 (한국어)