...
To correct this example, the struct big pointer is cast as a char *. This , which causes skip to be scaled by a factor of 1.:
| Code Block | ||||
|---|---|---|---|---|
| ||||
struct big {
unsigned long long ull_1; /* typically 8 bytes */
unsigned long long ull_2; /* typically 8 bytes */
unsigned long long ull_3; /* typically 8 bytes */
int si_4; /* typically 4 bytes */
int si_5; /* typically 4 bytes */
};
/* ... */
size_t skip = offsetof(struct big, ull_2);
struct big *s = (struct big *)malloc(sizeof(struct big));
if (!s) {
/* Handle malloc() error */
}
memset((char *)s + skip, 0, sizeof(struct big) - skip);
/* ... */
free(s);
s = NULL;
|
...
| CERT C++ Secure Coding Standard | EXP08-CPP. Ensure pointer arithmetic is used correctly |
| ISO/IEC TR 24772:2013 | Pointer Casting and Pointer Type Changes [HFC] Pointer Arithmetic [RVG] |
| ISO/IEC TS 17961 (Draft) | Adding or subtracting a byte count to an element pointer [cntradd] |
| MISRA - C:2012 | Rule 1718.1 (required) Rule 1718.2 (required) Rule 1718.3 (required) Rule 1718.4 (advisory) |
| MITRE CWE | CWE-468, Incorrect pointer scaling |
...