Rule Definition
A NULL pointer dereference occurs when the application dereferences a pointer that it expects to be valid, but is NULL, typically causing a crash or exit.
Remediation
Always check the object is not null before using it or it is not used in a try block.
Violation Code Sample
object foo = null;
if ( i > 0)
{
M1(foo.ToString()); // VIOLATION IT IS NULL
}
else
{
foo = new object();
}
M2(foo.ToString());
Fixed Code Sample
object foo = null;
if ( i > 0)
{
if(foo != null)
{
M1(foo.ToString()); // NO VIOLATION
}
}
else
{
foo = new object();
}
M2(foo.ToString());
Reference
https://cwe.mitre.org/data/definitions/476.html
Related Technologies
Technical Criterion
CWE-476 - NULL Pointer Dereference
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.