Static Analysis Problem Type Reference

Unchecked memory allocation

A pointer to allocated memory was not checked for null before dereference.

This occurs when a return value of malloc family allocation function or new (nothrow) operator is not checked for null before dereference. If memory allocation fails the malloc family allocation functions and new (nothrow) operator return null. If the pointer can be null, then all uses should be protected with a check for null.

ID

Code Location

Description

1

Allocation site

The place where the memory allocation may fail

2

Null dereference

The place where the null pointer was dereferenced

Example


#include <malloc.h>
 
int main(int argc, const char* argv[])
{
    int* p = (int*)malloc(sizeof(int));
    *p = 0; // oops: used arg1 without checking that is wasn't null
    free(p);
    return 0;

}