In this section:
Porting code between differing machine architectures can be difficult for many reasons. A particularly tricky problem occurs when the sizes of data objects, particularly pointers, differ from that for which the software was created. This error occurs when a pointer is cast to a type with fewer bits, causing information to be lost, and is designed to help in porting codes to architectures where pointers and integers are of different lengths.
|
The suppressEDGWarning off
option must be used in order for Insure++ to find this error.
Modern compilers will often catch this problem unless the user has explicitly forced the conversion with an appropriate type cast.
The following code shows a pointer being copied to a variable too small to hold all its bits.
/* * File: badcast.c */ main() { char q, *p; p = "Testing"; q = (char)p; return 0; } |
[badcast.c:9] **BAD_CAST** Cast of pointer loses precision: >> q = (char) p; ---- Associated Common Weakness Enumerations ---- CWE-704: Incorrect type conversion or cast |
This error normally indicates a significant portability problem that should be corrected by using a different type to save the pointer expression. In ANSI C the type void *
will always be large enough to hold a pointer value.
The table below shows Common Weakness Enumerations associated with this error.
CWE | Description |
---|---|
CWE-704 | Incorrect type conversion or cast |