Do not convert a pointer value to a pointer type that is more strictly aligned than the type the value actually points to. Different alignments are possible for different types of objects. If the type-checking system is overridden by an explicit cast or the pointer is converted to a void pointer (void *
) and then to a different type, the alignment of an object may be changed.
Subclause The C Standard, 6.3.2.3, paragraph 7 , of the C Standard [ISO/IEC 9899:2011], states:
A pointer to an object or incomplete type may be converted to a pointer to a different object or incomplete type. If the resulting pointer is not correctly aligned for the referenced type, the behavior is undefined.
...
Compliant Solution (C11, alignas()
)
This compliant solution uses alignas
to align the character c
to the alignment of an integeruses the alignment specifier to declare the char
object c
with the same alignment as that of an int
. As a result, the two pointers reference equally aligned pointer types:
...
Noncompliant Code Example
The C Standard allows a any object pointer to be cast into to and out of from void *
. As a result, it is possible to silently convert from one pointer type to another without the compiler diagnosing the problem by storing or casting a pointer to void *
and then storing or casting it to the final type. In this noncompliant code example, loop_function()
is passed the char
pointer loop_ptr
but returns an int
pointer:
...
Noncompliant Code Example
Many Some architectures require that pointers are correctly aligned when accessing objects larger than a byte. It However, it is common is system code , however, that unaligned data (for example, the network stacks) must be copied to a properly aligned memory location, such as in this noncompliant code example:
Code Block | ||||
---|---|---|---|---|
| ||||
#include <string.h> struct foo_header { int len; /* ... */ }; void func(char *data, size_t offset) { struct foo_header *tmp; struct foo_header header; tmp = data + offset; memcpy(&header, tmp, sizeof(header)); /* ... */ } |
Unfortunately, assigning Assigning an unaligned value to a pointer that references a type that needs to be aligned is undefined behavior. An implementation may notice, for example, that tmp
and header
must be aligned and use an inline memcpy()
that uses instructions that assume aligned data.
Compliant Solution
This compliant solution does not avoids the use of the foo_header
pointer:
Code Block | ||||
---|---|---|---|---|
| ||||
#include <string.h> struct foo_header { int len; /* ... */ }; void func(char *data, size_t offset) { struct foo_header header; memcpy(&header, data + offset, sizeof(header)); /* ... */ } |
Exceptions
EXP36-EX1: Some platforms, notably x86, hardware architectures have relaxed requirements with regard to pointer alignment. Using a pointer that is not properly aligned is correctly handled by the platformarchitecture, although there might be a performance penalty. On such a platforman architecture, improper pointer alignment is permitted, as it is but remains an efficiency problem but not a security problem.
Risk Assessment
Accessing a pointer or an object that is not properly aligned can cause a program to crash or give wrong erroneous information, or it can cause slow pointer accesses (if the architecture allows misaligned accesses).
...
[Bryant 2003] | |
[ISO/IEC 9899:2011] | Subclause 6.3.2.3, "Pointers" |
[Walfridsson 2003] | Aliasing, Pointer Casts and GCC 3.3 |
...