Versions Compared

Key

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

...

A method should not return a value or status code that does not accurately specify the problem. Clients should be able to rely on the value for performing critical decisions.

Noncompliant code example

As shown in this example, noncompliant methods can silently corrupt the state of the object if they do not return a value that the developer can intuitively interpret.

Code Block
bgColor#FFCCCC
public void updateNode(int id, int newValue){		
  Node current = root;
  while(current != null){
    if(current.getId() == id){
      current.setValue(newValue);
      break;
    }
    current = current.next;
  }
}

Compliant solution

This compliant solution recommends returning the result of the operation; true for success and false for failure.

Code Block
bgColor#CCCCFF
public boolean updateNode(int id, int newValue){		
  Node current = root;
  while(current != null){
    if(current.getId() == id){
      current.setValue(newValue);
      return true; // Node successfully updated
    }
    current = current.next;
  }
  return false;
}

Compliant solution

This compliant solution returns the updated Node so that the developer can simply check for a null value lest the operation fails. Appropriate return values for methods can vary depending on the control flow or the information that the developer finds more useful.

Code Block
bgColor#CCCCFF
public Node updateNode(int id, int newValue){	
  Node current = root;
  while(current != null){
    if(current.getId() == id){
      current.setValue(newValue);
      return current;
    }
    current = current.next;
  }
  return null;
}

Compliant solution

This solution combines the best of both worlds - exceptions and status codes. In this case, an exception is thrown if the operation is not successful. This ensures that the client has to handle the event wherein the Node is not found. If the Node is found, it is updated and returned.

Code Block
bgColor#CCCCFF
public Node updateNode(int id, int newValue) throws IdNotFoundException {
  Node current = root;
  while(current != null){
    if(current.getId() == id){
      current.setValue(newValue);
      return current;
    }
    current = current.next;
  }	
  throw new NodeNotFoundException();
}

Risk assesment

Failure to provide appropriate feedback through status codes or exceptions can lead to inconsistent object state and unexpected program behavior.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

MET04- J

medium

probable

medium

P8

L2

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

References

Wiki Markup
\[[Ware 08|AA. Java References#Ware 08]\]
\[[MITRE 09|AA. Java References#MITRE 09]\] [CWE ID 393|http://cwe.mitre.org/data/definitions/393.html] "Return of Wrong Status Code" and [CWE ID 389|http://cwe.mitre.org/data/definitions/393.html] "Error Conditions, Return Values, Status Codes"

...