...
Noncompliant Code Example
In this noncompliant example, the char pointer &c
is converted to the more strictly aligned int
pointer i_ptr
.
Code Block | ||||
---|---|---|---|---|
| ||||
void f(void) {
int *i_ptr;
char c;
i_ptr = (int *)&c; /* violation */
/* ... */
}
|
Compliant Solution
In this compliant solution, the value referenced by the char
pointer c_ptr
has the alignment of type int
.
Code Block | ||||
---|---|---|---|---|
| ||||
void f(void) {
char *c_ptr;
int *i_ptr;
int i;
c_ptr = (char *)&i;
i_ptr = (int *)c_ptr;
/* ... */
} |
Noncompliant Code Example
The C Standard allows a pointer to be cast into and out of 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, the type checking system is circumvented due to the caveats of void
pointers loop_function()
is passed the char
pointer loop_ptr
but returns an int
pointer.
Code Block | ||||
---|---|---|---|---|
| ||||
char *loop_ptr; int *int_ptr; int *loop_function(void *v_pointer) { /* ... */ return v_pointer; } int_ptr = loop_function(loop_ptr); |
This example compiles without warning. However, v_pointer
can be aligned on a 1-byte boundary.
Compliant Solution
Because the input parameter directly influences the return value, and loop_function()
returns an int *
, the formal parameter v_pointer
is redeclared to only accept int *
.
...
Another solution is to ensure that loop_ptr
points to an object returned by malloc()
because this object is guaranteed to be aligned properly for any need. However, this is a subtlety that is easily missed when the program is modified in the future. It is easier and safer to let the type system document the alignment needs.
Noncompliant Code Example
Many architectures require that pointers are correctly aligned when accessing objects bigger larger than a byte. There are, however, many places in system code where you receive unaligned data (for example, the network stacks) that needs to be copied to a properly aligned memory location, such as in this noncompliant code example.
...
Unfortunately, the behavior is undefined when you assign an unaligned value to a pointer that points to a type that needs to be aligned. An implementation may notice, for example, that tmp
and header
must be aligned, so it could use an inlined memcpy()
that uses instructions that assumes aligned data.
Compliant Solution
This compliant solution does not use the foo_header
pointer.
Code Block | ||||
---|---|---|---|---|
| ||||
char *data;
struct foo_header header;
memcpy(&header, data + offset, sizeof(header));
if (header.len < FOO)
/* ... */
|
Noncompliant Code Example
In this noncompliant example, the char pointer &c is converted to the more strictly aligned int pointer i_ptr.
Code Block | ||||
---|---|---|---|---|
| ||||
void f(void) {
int *i_ptr;
char c;
i_ptr = (int *)&c; // diagnostic required
/* ... */
}
|
Compliant Solution
This compliant solution because the value referenced by the char pointer c_ptr has the alignment of type int.
Code Block | ||||
---|---|---|---|---|
| ||||
void f(void) { char *c_ptr; int *i_ptr; int i; c_ptr = (char *)&i; i_ptr = (int *)c_ptr; /* ... */ } |
Noncompliant Code Example
...