...
Typically, the amount of memory to allocate will be the size of the type of object to allocate. When allocating space for an array, the size of the object will be multiplied by the bounds of the array. When allocating space for a structure containing a flexible array member, the size of the array member must be added to the size of the structure. (see See MEM33-C. Allocate and copy structures containing a flexible array member dynamically.) . Use the correct type of the object when computing the size of memory to allocate.
...
In this noncompliant code example, an array of long is allocated and assigned to p. The code checks for unsigned integer overflow in compliance with INT32-C. Ensure that operations on signed integers do not result in overflow and also ensures that len is not equal to zero. (see See MEM04-C. Beware of zero-length allocations.) . However, because sizeof(int) is used to compute the size, and not sizeof(long), an insufficient amount of memory can be allocated on implementations where sizeof(long) is larger than sizeof(int) .
...
Tool | Version | Checker | Description | ||||||
|---|---|---|---|---|---|---|---|---|---|
| CodeSonar |
| ALLOC.SIZE.ADDOFLOW | Addition overflow of allocation size | ||||||
|
| Could check violations of this rule by examining the size expression to
| |||||||
| BAD_ALLOC_STRLEN
| Can find instances where string length is miscalculated (length calculated may be one less than intended) for memory allocation purposes. Coverity Prevent cannot discover all violations of this rule, so further verification is necessary Finds memory allocations that are assigned to a pointer that reference objects larger than the allocated block | |||||||
5.0 |
| Can detect violations of this rule with CERT C Rule Pack, except those involving the | |||||||
| 9.1 | INCORRECT.ALLOC_SIZE | ||||||||
| 400 S, 487 S, 115 D | Enhanced enforcement | |||||||
| 3.1.1 | |||||||||
| Parasoft C/C++test | 9.5 | MRM-45 | Partially implemented | ||||||
| Polyspace Bug Finder | R2016a | Memory allocation with tainted size, Pointer access out of bounds, Wrong type used in sizeof | Size argument to memory function is from an unsecure source Pointer dereferenced outside its bounds
| ||||||
| PRQA QA-C |
| 696 |
...