Null pointer dereferencing occurs when a variable bound to the null
value is treated as if it were a valid object reference and used without checking its state. This condition results in a NullPointerException
, which can in turn result in a denial of service. The denial of service is caused by the exception interrupting the program or thread, and probably causing it to terminate. Termination is likely because catching NullPointerExceptions
NullPointerException
is forbidden by ERR08-J. Do not catch NullPointerException or any of its ancestors. Consequently, code must never dereference null pointers. Note that the context in which code is defined can impact the determination of conformance to this rule, as shown in the second Noncompliant Code Example and (corresponding) second Compliant Solution.
Noncompliant Code Example
...
Code Block | ||
---|---|---|
| ||
public boolean isName(String s) { String names[] = s.split(" "); if (names.length != 2) { return false; } return (isCapitalized(names[0]) && isCapitalized(names[1])); } |
Method isName
is not compliant with this rule ()
is noncompliant because a null
argument will result results in isName()
dereferencing a null pointer.
...
This compliant solution demonstrates that the context in which code appears can impact its compliance. This example includes the same isName()
method as abovethe previous noncompliant example, but this time as part of a more general method that tests string arguments. The isName()
method is also now marked private
.
Code Block | ||
---|---|---|
| ||
public class Foo { private boolean isName(String s) { String names[] = s.split(" "); if (names.length != 2) { return false; } return (isCapitalized(names[0]) && isCapitalized(names[1])); } public boolean testString(String s) { if (s == null) return false; else return isName(s); } } |
In this example, we have made The isName()
method is a private method with only one caller in its containing class. The calling method, testString()
, is written so as to guarantee guarantees that isName()
is always called with a valid string reference. ThereforeAs a result, the class as whole conforms with this rule, even though isName()
in isolation does not. In general, inter-procedural reasoning of this sort is an acceptable approach to avoiding null pointer dereferences.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9531491da3246f9e-ed81a53a-425d4b2f-aa83ba1a-22d2db4555145ec955a94bcb"><ac:plain-text-body><![CDATA[ | [ISO/IEC TR 24772:2010 | http://www.aitcnet.org/isai/] | Null Pointer Dereference [XYH] | ]]></ac:plain-text-body></ac:structured-macro> |
CWE-476. NULL pointer dereference |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="13bc7e2d41351a55-eb2afcc2-4f564bb5-97e4892d-cddeb67920a09eae6022b38b"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. References#API 06]] | [Method | http://java.sun.com/javase/6/docs/api/java/security/AccessController.html#doPrivileged(java.security.PrivilegedAction)] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a5d31761a24d9b97-353189e6-41494ae1-bd72aa75-235a16d78cda06cc43ccd762"><ac:plain-text-body><![CDATA[ | [[Hovemeyer 2007 | AA. References#Hovemeyer 07]] |
| ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0779470aa0ca6504-d7adcf14-48a3454b-83f285bd-842e284706e464758fc62e38"><ac:plain-text-body><![CDATA[ | [[Reasoning 2003 | AA. References#Reasoning 03]] | Defect ID 00-0001 | ]]></ac:plain-text-body></ac:structured-macro> | |
| Null Pointer Dereference | ||||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a57bfdcf52b7b6c5-6ba0ecd0-4c5744b7-bc24a8d2-96d699cda90bd5cd9f52d0a6"><ac:plain-text-body><![CDATA[ | [[SDN 2008 | AA. References#SDN 08]] | [Bug ID 6514454 | http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6514454] | ]]></ac:plain-text-body></ac:structured-macro> |
...