Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This noncompliant code example defines an isName() method that takes a String argument and returns true if the given string is a valid name. A valid name is defined as two capitalized words separated by one or more spaces.

Code Block
bgColor#FFcccc
public boolean isName(String s) {
  String names[] = s.split(" ");
  if (names.length != 2) {
    return false;
  }
  return (isCapitalized(names[0]) && isCapitalized(names[1]));
}

...

This compliant solution demonstrates that the context in which code appears can impact its compliance. This example includes the same isName method as above, but this time as part of a more general method that tests string arguments. The isName() method is also now marked private.

Code Block
bgColor#FFcccc
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);
  }
}

...

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="838c9373e6f008a0-ef64ef49-40044797-a233be92-42d2f8f2273d63a38339ea48"><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="306782158f70d5a8-33459a7c-40db4a89-b46f9023-0b19b068140033f186abfb7a"><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="a70e9e8d915ed51e-278ce188-42d94ca2-b2979850-4cc751f502599f3bf8ed6f45"><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="9ad200e3e52ba960-71a0f34d-478d4778-ba4093d7-cb9a029c58ee64d95620338d"><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="36f8b961c8012308-d61365f4-46304141-8290b4ea-f057be402b322ca6e7f857f9"><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>

...