...
Non-Compliant Code Example 3
In this example, an array of long integers is allocated and assigned to {{Wiki Markup p}}. However, {{sizeof(int)}} is used to size the allocated memory. If {{sizeof(long)}} is larger than {{sizeof(int)}}, then an insufficient amount of memory will be is allocated. This example also checks for unsigned numeric overflow in compliance with \[[INT32-C|INT32-C. Ensure that integer operations do not result in an overflow]\].
| Code Block | ||
|---|---|---|
| ||
void function(size_t len) {
long *p;
if (len > SIZE_MAX / sizeof(long)) {
/* handle overflow */
}
p = malloc(len * sizeof(int));
if (p == NULL) {
/* handle error */
}
/* ... */
free(p);
}
|
...