"It is undefined what will happen if a pointer of some type is converted to void*, and then the void* pointer is converted to a type with a stricter alignment requirement" -C99 rationaleV5.10.pdfArchitectures are free to define the byte alignment of the fundamental types of C. Typically, there are several different possible alignments used by the fundamental types (proportional to their size). By definition of C99, a pointer may be cast into and out of void*
without warning. Thus it is possible to silently switch from one type of pointer to another without a warning by first storing or casting the initial pointer to void*
and then storing or casting it to the final type. Because of all of these things, it is possible to silently switch the pointer type, which means that if that pointer is dereferenced and the alignment is different, it might cause the program to terminate abnormally.
Non-compliant Code Example
Code Block | ||
---|---|---|
| ||
char *loop_ptr; int * int_ptr; int *mkVoidPtrloop_function(void *v_pointer){ return v_pointer; } int_ptr = mkVoidPtrloop_function(loop_ptr); |
In this example the This example should compile without warning, but v_pointer
might be aligned on a 1 byte boundary. Once it is cast to an int some architectures will require it to be on 4 byte boundaries. Pointers are often cast because a void*
cannot be dereferenced. Careless coding can result in an arbitrary pointer type being used irregardless of its alignment.
Implementation Details
List of common alignments for Microsoft, Borland and GNU compilers to x86
Type | Alignment |
---|---|
| 1 byte aligned |
| 2 byte aligned |
| 4 byte aligned |
| 4 byte aligned |
| 8 byte on Windows, 4 byte on Linux |
Compliant Solution
Make specific functions (avoid use of void*)
or
use offsetof from <stddef.h> to create an alignof function that will return the alignment
of anything that is not:
- An lvalue representing a bit field
- A function type
- An undefined structure or class
- An incomplete type (such as void)
If int_ptr
is then later dereferenced, the program might be killed.
Compliant Solution
In this compliant solution, the parameter is changed to only accept other int*
pointers since the input parameter directly influences the output parameter.
Code Block | ||
---|---|---|
| ||
int *loop_ptr; int * int_ptr; int *loopFunction(int *v_pointer) { { return v_pointer; } int_ptr = loopFunction(loop_ptr); |
Implementation Details
List of common alignments for Microsoft, Borland, and GNU compilers to x86
Type | Alignment |
---|---|
| 1 byte aligned |
| 2 byte aligned |
| 4 byte aligned |
| 4 byte aligned |
| 8 byte on Windows, 4 byte on Linux |
Risk Assessment
Accessing a pointer that is no longer on the correct access boundary can cause a program to crash, give wrong information or have slow pointer accesses (if the architecture does not care about alignment).
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
DRAFT | 1 (low) | 2 (probable) | 2 (medium) | P4 | L3 |
References
Wiki Markup |
---|
\[[Bryant |
...
03|AA. C References#Bryant 03]\]
\[[ISO/IEC 9899-1999:TC2|AA. C References#ISO/IEC 9899-1999TC2]\] Section 6.2.5, "Types" |