Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added exception for null method arguments

...

Code Block
bgColor#FFcccc
public static int cardinality(Object obj, final Collection col) {
  int count = 0;
  if (col == null) {
    return count;
  }
  Iterator it = col.iterator();
  while (it.hasNext()) {
    Object elt = it.next();
    if ((null == obj && null == elt) || obj.equals(elt)) {  // null pointer dereference
      count++;
    }
  }
  return count;
}

...

Code Block
bgColor#ccccff
public static int cardinality(Object obj, final Collection col) {
  int count = 0;
  if (col == null) {
    return count;
  }
  Iterator it = col.iterator();
  while (it.hasNext()) {
    Object elt = it.next();
    if ((null == obj && null == elt) || 
        (null != obj && obj.equals(elt))) {
      count++;
    }
  }
  return count;
}

Explicit null checks as shown here an acceptable approach to eliminating null pointer dereferences.

Exceptions

EXP01-EX0: A method may dereference an object without testing it for null if the following conditions hold:

  • The object must be a method argument, and not previously accessed
  • There does not exist a better alternate behavior for handling null objects. That is, the method may choose not to do the null check for performance reasons, or it may not have a better alternative for handling the null object.
  • The method must provide API documentation to this effect; that it does not gracefully handle null objects.

Risk Assessment

Dereferencing a null pointer can lead to a denial of service. In multithreaded programs, null pointer dereferences can violate cache coherency policies and can cause resource leaks.

...

CERT C Secure Coding Standard

EXP34-C. Do not dereference null pointers

CERT C++ Secure Coding Standard

EXP34-CPP. Ensure a null pointer is not dereferenced

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="2f18e37bc859dd1f-99ba11bb-4ebe477d-b0d593a2-5bbf7f8ec5ed145eeba2ef1f"><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>

MITRE CWE

CWE-476. NULL pointer dereference

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ce40a2ba39871edb-46e5ae2d-4c7c42c1-be7a8587-fca2eb76788fdaa8fbb01822"><ac:plain-text-body><![CDATA[

[[API 2006

AA. References#API 06]]

[Method doPrivileged()

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="54eee2543365f605-8e8b319e-447645fa-8fb1b6fd-72204013d5a99379a8aa6452"><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="3af02b46d26951ce-15affcad-467c464c-bbb78b08-1dd2f67f3d30201f01eae1d1"><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="d310c74fada9deca-37451ed0-49b546b3-a82c8b4c-d20493e85d229d372956b802"><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>

...