Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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.

Section Subclause 6.3.2.3, para. paragraph 7, of the C Standard [ISO/IEC 9899:2011] states:

...

If the misaligned pointer is dereferenced, the program may terminate abnormally. The cast alone may cause a loss of information , even if the value is not dereferenced. For example, the assertion in the following code example will fail on some conforming implementations , even though no pointers are dereferenced:

Code Block
bgColor#FFCCCC
langc
char c = 'x';
int *ip = (int *)&c; /* thisThis can lose information */
char *cp = (char *)ip;
assert(cp == &c);    /* willWill fail on some conforming implementations */

...

Code Block
bgColor#FFCCCC
langc
void f(void) {
  int *i_ptr;
  char c;
 
  i_ptr = (int *)&c;  /* violationViolation */
  /* ... */
}

Compliant Solution

...

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, loop_function() is passed the char pointer  loop_ptr but returns an int pointer.:

Code Block
bgColor#FFCCCC
langc
char *loop_ptr;
int *int_ptr;

int *loop_function(void *v_pointer) {
  /* ... */
  return v_pointer;
}
int_ptr = loop_function(loop_ptr);

...

Because the input parameter directly influences the return value, and loop_function() returns an int *, the formal parameter v_pointer is redeclared to accept only accept int *:

Code Block
bgColor#ccccff
langc
int *loop_ptr;
int *int_ptr;

int *loop_function(int *v_pointer) {
  /* ... */
  return v_pointer;
}
int_ptr = loop_function(loop_ptr);

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.

...

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 assume aligned data.

Compliant Solution

...

Code Block
bgColor#FFCCCC
langc
char c = 'x';
int *ip = (int *)&c; /* thisThis can lose information */
char *cp = (char *)ip;
assert(cp == &c);    /* willWill fail on some conforming implementations */

...

Code Block
bgColor#ccccff
langc
alignas(int) char c = 'x'; /* alignAlign c to the alignment of an int */
int *ip = (int *)&c; 
char *cp = (char *)ip;
assert(cp == &c);    /* bothBoth cp and &c point to equally aligned objects */

...

Tool

Version

Checker

Description

Compass/ROSE  

Can detect violations of this rule. However, it does not flag explicit casts to void * and then back to another pointer type

ECLAIR

Include Page
ECLAIR_V
ECLAIR_V

CC2.EXP36

Fully implemented
EDG   
GCC
Include Page
GCC_V
GCC_V
 

Can detect some violations of this rule when the -Wcast-align flag is used

LDRA tool suite

Include Page
LDRA_V
LDRA_V

94 S
540 S

Fully implemented
PRQA QA-C
Include Page
PRQA_V
PRQA_V
3305Fully implemented

...

...