...
The C Standard, 6.3.2.3, paragraph 7 [ISO/IEC 9899:20112024], 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 the referenced type, the behavior is undefined.
...
EXP36-C-EX1: Some hardware architectures have relaxed requirements with regard to pointer alignment. Using a pointer that is not properly aligned is correctly handled by the architecture, although there might be a performance penalty. On such an architecture, improper pointer alignment is permitted but remains an efficiency problem.
The x86 32- and 64-bit architectures usually impose only a performance penalty for violations of this rule, but under some circumstances, noncompliant code can still exhibit undefined behavior. Consider the following program:
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdio.h>
#include <stdint.h>
#define READ_UINT16(ptr) (*(uint16_t *)(ptr))
#define WRITE_UINT16(ptr, val) (*(uint16_t *)(ptr) = (val))
void compute(unsigned char *b1, unsigned char *b2,
int value, int range) {
int i;
for (i = 0; i < range; i++) {
int newval = (int)READ_UINT16(b1) + value;
WRITE_UINT16(b2, newval);
b1 += 2;
b2 += 2;
}
}
int main() {
unsigned char buffer1[1024];
unsigned char buffer2[1024];
printf("Compute something\n");
compute(buffer1 + 3, buffer2 + 1, 42, 500);
return 0;
} |
This code tries to read short ints (which are 16 bits long) from odd pairs in a character array, which violates this rule. On 32- and 64-bit x86 platforms, this program should run to completion without incident. However, the program aborts with a SIGSEGV due to the unaligned reads on a 64-bit platform running Debian Linux, when compiled with GCC 4.9.4 using the flags -O3
or -O2 -ftree-loop-vectorize -fvect-cost-model
.
If a developer wishes to violate this rule and use undefined behavior, they must not only ensure that the hardware guarantees the behavior of the object code, but they must also ensure that their compiler, along with its optimizer, also respect these guarantees.
EXP36-C-EX2: If a pointer is known to be correctly aligned to the target type, then a cast to that type is permitted. There are several cases where a pointer is known to be correctly aligned to the target type. The pointer could point to an object declared with a suitable alignment specifier. It could point to an object returned by aligned_alloc()
, calloc()
, malloc()
, or realloc()
, as per the C standard, section 7.22.3, paragraph 1 [ISO/IEC 9899:2011].
...
Accessing a pointer or an object that is not properly aligned can cause a program to crash or give erroneous information, or it can cause slow pointer accesses (if the architecture allows misaligned accesses).
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
EXP36-C | Low | Probable | Medium | P4 | L3 |
Automated Detection
Tool | Version | Checker | Description | ||||
---|---|---|---|---|---|---|---|
Astrée |
|
pointer-cast-alignment | Fully checked | ||||||||
Axivion Bauhaus Suite |
| CertC-EXP36 | |||||||
CodeSonar |
| LANG.CAST.PC.OBJ | Cast: Object Pointers | ||||||
Compass/ROSE |
Can detect violations of this rule. However, it does not flag explicit casts to | |||||||||
Coverity |
| MISRA C 2004 Rule 11.4 MISRA C 2012 Rule 11.1 MISRA C 2012 Rule 11.2 MISRA C 2012 Rule 11.5 MISRA C 2012 Rule 11.7 | Implemented | ||||||
Cppcheck Premium |
| premium-cert-exp36-c | Partially implemented | ||||||
| CC2.EXP36 | Fully implemented | |||||||
EDG |
GCC |
|
Can detect some violations of this rule when the | |||||||||
Helix QAC |
| C0326, C3305 C++3033, C++3038 | |||||||
Klocwork |
| MISRA.CAST. |
MISRA.CAST.PTR
OBJ_PTR_TO |
PORTING.CAST.PTR.FLTPNT
PORTING.CAST.PTR
PORTING.CAST.PTR.SIZE
PORTING.CAST.SIZE
_OBJ_PTR.2012 |
LDRA tool suite |
| 94 S, 606 S | Partially implemented | ||||||
Parasoft C/C++test |
| CERT_C-EXP36-a | Do not cast pointers into more strictly aligned pointer types | |||||||
PC-lint Plus |
| 2445 | Partially supported: reports casts directly from a pointer to a less strictly aligned type to a pointer to a more strictly aligned type | ||||||
Polyspace Bug Finder |
| Checks for source buffer misaligned with destination buffer (rule fully covered) | |||||||
PVS-Studio |
| V548, V641, V1032 | |||||||
RuleChecker |
| pointer-cast-alignment | Fully checked |
3033, 3038
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
...
Key here (explains table format and definitions)
Taxonomy | Taxonomy item | Relationship |
---|---|---|
CERT C | VOID EXP56-CPP. Do not cast pointers into more strictly aligned pointer types | Prior to 2018-01-12: CERT: Unspecified Relationship |
ISO/IEC TR 24772:2013 | Pointer Casting and Pointer Type Changes [HFC] | Prior to 2018-01-12: CERT: Unspecified Relationship |
ISO/IEC TS 17961 | Converting pointer values to more strictly aligned pointer types [alignconv] | Prior to 2018-01-12: CERT: Unspecified Relationship |
MISRA C:2012 | Rule 11.1 (required) | Prior to 2018-01-12: CERT: Unspecified Relationship |
MISRA C:2012 | Rule 11.2 (required) | Prior to 2018-01-12: CERT: Unspecified Relationship |
MISRA C:2012 | Rule 11.5 (advisory) | Prior to 2018-01-12: CERT: Unspecified Relationship |
MISRA C:2012 | Rule 11.7 (required) | Prior to 2018-01-12: CERT: Unspecified Relationship |
Bibliography
[Bryant 2003] |
[ISO/IEC 9899: |
2024] | 6.3.2.3, "Pointers" |
[Walfridsson 2003] | Aliasing, Pointer Casts and GCC 3.3 |
...
...