Pointer returned from dynamic allocation not checked for NULL or
nullptr value
This defect occurs when you access dynamically allocated memory without first checking if the prior memory allocation succeeded.
When memory is dynamically allocated using malloc,
calloc, or realloc, it returns a value
NULL if the requested memory is not available. If the code
following the allocation accesses the memory block without checking for this
NULL value, this access is not protected from
failures.
Check the return value of malloc, calloc, or
realloc for NULL before accessing the allocated memory
location.
int *ptr = malloc(size * sizeof(int));
if(ptr) /* Check for NULL */
{
/* Memory access through ptr */
}| Group: Dynamic memory |
| Language: C | C++ |
| Default: Off |
Command-Line Syntax: UNPROTECTED_MEMORY_ALLOCATION |
| Impact: Low |
| CWE ID: 253, 690, 789 |