NULL pointer dereference

NULL pointer dereference

Description

Dereferencing a null pointer in C or C++ has undefined behavior.

Potential impact

A crash can interrupt service. Error responses can also expose internal details.

Remediation

Check a potentially null pointer before reading or writing through it. If it is null, return or handle the error.

Examples

Before

c
char *p = NULL;
p[0] = 'x';

After

c
char *p = get_buffer();
if (p == NULL) { return; }
p[0] = 'x';

The example skips the write when get_buffer() returns NULL. Any buffer it returns must still be valid and have at least one writable byte at the time of the p[0] assignment.

References