ISO/IEC TS 17961 [alignconv]
Converting pointer values to more strictly aligned pointer types
Description
Rule Definition
Converting pointer values to more strictly aligned pointer types.1
Polyspace Implementation
This checker checks for Wrong allocated object size for cast.
Examples
Wrong allocated object size for cast occurs during pointer conversion when the pointer’s address is misaligned. If a pointer is converted to a different pointer type, the size of the allocated memory must be a multiple of the size of the destination pointer.
Dereferencing a misaligned pointer has undefined behavior and can cause your program to crash.
Suppose you convert a pointer ptr1 to ptr2.
If ptr1 points to a buffer of N bytes and
ptr2 is a pointer where
type
*sizeof( is
type)n bytes, make sure that N is an integer
multiple of n.
See examples of fixes below.
If you do not want to fix the issue, add comments to your result or code to avoid another review. See:
Address Results in Polyspace User Interface Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
#include <stdlib.h>
void dyn_non_align(void){
void *ptr = malloc(13);
long *dest;
dest = (long*)ptr; //defect
}In this example, the software raises a defect on the
conversion of ptr to a long*. The dynamically
allocated memory of ptr, 13 bytes, is not a multiple of the size of
dest, 4 bytes. This misalignment causes the Wrong allocated object size for cast defect.
One possible correction is to use a pointer size that is a multiple of the destination size. In this example, resolve the defect by changing the allocated memory to 12 instead of 13.
#include <stdlib.h>
void dyn_non_align(void){
void *ptr = malloc(12);
long *dest;
dest = (long*)ptr;
}void static_non_align(void){
char arr[13], *ptr;
int *dest;
ptr = &arr[0];
dest = (int*)ptr; //defect
}In this example, the software raises a defect on the conversion
of ptr to an int* in line 6. ptr has
a memory size of 13 bytes because the array arr has
a size of 13 bytes. The size of dest is 4 bytes,
which is not a multiple of 13. This misalignment causes the Wrong allocated object size for cast defect.
One possible correction is to use a pointer size that is a multiple
of the destination size. In this example, resolve the defect by changing
the size of the array arr to a multiple of 4.
void static_non_align(void){
char arr[12], *ptr;
int *dest;
ptr = &arr[0];
dest = (int*)ptr;
}#include <stdlib.h>
void *my_alloc(int size) {
void *ptr_func = malloc(size);
if(ptr_func == NULL) exit(-1);
return ptr_func;
}
void fun_non_align(void){
int *dest1;
char *dest2;
dest1 = (int*)my_alloc(13); //defect
dest2 = (char*)my_alloc(13); //not a defect
}In this example, the software raises a defect on the conversion
of the pointer returned by my_alloc(13) to an int* in
line 11. my_alloc(13) returns a pointer with a
dynamically allocated size of 13 bytes. The size of dest1 is
4 bytes, which is not a divisor of 13. This misalignment causes the Wrong allocated object size for cast defect.
In line 12, the same function call, my_alloc(13),
does not call a defect for the conversion to dest2 because
the size of char*, 1 byte, a divisor of 13.
One possible correction is to use a pointer size that is a multiple
of the destination size. In this example, resolve the defect by changing
the argument for my_alloc to a multiple of 4.
#include <stdlib.h>
void *my_alloc(int size) {
void *ptr_func = malloc(size);
if(ptr_func == NULL) exit(-1);
return ptr_func;
}
void fun_non_align(void){
int *dest1;
char *dest2;
dest1 = (int*)my_alloc(12);
dest2 = (char*)my_alloc(13);
}Check Information
| Decidability: Undecidable |
Version History
Introduced in R2019a
1 Extracts from the standard "ISO/IEC TS 17961 Technical Specification - 2013-11-15" are reproduced with the agreement of AFNOR. Only the original and complete text of the standard, as published by AFNOR Editions - accessible via the website www.boutique.afnor.org - has normative value.
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.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
Cómo obtener el mejor rendimiento
Seleccione China (en idioma chino o inglés) para obtener el mejor rendimiento. Los sitios web de otros países no están optimizados para ser accedidos desde su ubicación geográfica.
América
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)