Versions Compared

Key

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

...

Non-Compliant Code Example 3

Wiki MarkupIn this example, an array of long integers is allocated and assigned to {{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
bgColor#FFcccc
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);
}

...