Why is this an issue?
A pointer to null (the 0 memory address) should never be dereferenced/accessed. Doing so will at best cause abrupt program termination, without the
ability to run any cleanup processes. At worst, it could expose debugging information that would be useful to an attacker or it could allow an
attacker to bypass security measures.
Noncompliant code example
char *p1 = ... ;
if (p1 == NULL && *p1 == '\t') { // Noncompliant, p1 will be dereferenced IFF it is null
// ...
}
char *p2 = ... ;
if (p2 != NULL) {
// ...
}
*p2 = '\t'; // Noncompliant; potential null-dereference
char *p3, *p4;
p3 = NULL;
// ...
p4 = p3;
*p4 = 'a'; // Noncompliant
Compliant solution
char *p1 = ... ;
if (p1 != NULL && *p1 == '\t') { // Compliant, *p1 cannot be evaluated when p1 is NULL
// ...
}
char *p2 = ... ;
if (p2 != NULL) {
// ...
*p2 = '\t'; // Compliant
}
Resources