...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stdlib.h>
#include <string.h>
void func(void) {
size_t resize = 1024;
size_t alignment = 1 << 12;
int *ptr;
int *ptr1;
if (NULL == (ptr = (int *)aligned_alloc(alignment,
sizeof(int)))) {
/* Handle error */
}
if (NULL == (ptr1 = (int *)aligned_alloc(alignment,
resize))) {
/* Handle error */
}
if (NULL == (memcpy(ptr1, ptr, sizeof(int))) {
/* Handle error */
}
free(ptr);
} |
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <malloc.h>
void func(void) {
size_t alignment = 1 << 12;
int *ptr;
int *ptr1;
/* Original allocation */
if (NULL == (ptr = (int *)_aligned_malloc(sizeof(int),
alignment))) {
/* Handle error */
}
/* Reallocation */
if (NULL == (ptr1 = (int *)_aligned_realloc(ptr, 1024,
alignment))) {
_aligned_free(ptr);
/* Handle error */
}
_aligned_free(ptr1);
} |
Note that the The size and alignment arguments for _aligned_malloc() are provided in reverse order of the C Standard aligned_alloc() function.
...
Improper alignment can lead to arbitrary memory locations being accessed and written to.
Recommendation | Severity | Likelihood | Detectable |
|---|
Repairable | Priority | Level | |
|---|---|---|---|
MEM36-C | Low | Probable | No |
No | P2 | L3 |
Automated Detection
Tool | Version | Checker | Description | ||||||
|---|---|---|---|---|---|---|---|---|---|
| Astrée |
| Supported, but no explicit checker | |||||||
| Axivion Bauhaus Suite |
| CertC-MEM36 | Fully implemented | ||||||
| CodeSonar |
| BADFUNC.REALLOC | Use of realloc | ||||||
| Cppcheck Premium |
| premium-cert-mem36-c | |||||||
| Helix QAC |
| C5027 C++5034 | |||||||
| Klocwork |
| AUTOSAR.STDLIB.MEMORY | |||||||
| LDRA tool suite |
| 44 S | Enhanced |
| enforcement | |||||||||
| Parasoft C/C++test |
| CERT_C-MEM36-a | Do not modify the alignment of objects by calling realloc() | ||||||
| CERT C: Rule MEM36-C | Checks for alignment change after memory allocation (rule fully covered) |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Bibliography
...
...