Tuesday, June 22, 2010

Dereference of null pointer

XCode Build and Analyze command finds several "Deference of null pointer" warnings in some 3rd party sources.

Pointer with a possible value NULL is used like it always points to a valid memory area:
#include <stddef.h>
int *p = NULL;
*p;
Trying to use a null pointer *p will cause undefined behaviour, depending on compiler and operating system. Reading from it might return garbage values, writing to it might corrupt memory somewhere else. Null pointer errors have also been used to go around security systems. Protected mode OSes usually stop the application trying to do such things, causing application to crash instead of the OS.

How to fix: check pointer value before trying to use it. Code safe.

No comments:

Post a Comment