Methods should always try to return a value that allows the developer to learn about the current state of the object and/or the result of an operation. This advice is consistent with guideline EXP00-J. Do not ignore values returned by methods. The returned value should be as representative as possible of the last known state and should be chosen with the perceptions and mental model of the developer in mind.
...
A method should not return a value or error code that does not accurately specify the object state. 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 | ||
---|---|---|
| ||
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 returns the result of the operation: true
for success and false
for failure.
Code Block | ||
---|---|---|
| ||
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. Return values for methods can vary depending on the control flow or the information that the developer finds more useful.
Code Block | ||
---|---|---|
| ||
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 worlds—exceptions and status codes. In this case, an exception is thrown if the operation is unsuccessful. The exception 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 | ||
---|---|---|
| ||
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 Assessment
Failure to provide appropriate feedback through return values, error codes, and exceptions can lead to inconsistent object state and unexpected program behavior.
Guideline | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
MET09MET54-J JG | medium | probable | medium | P8 | L2 |
Related
...
Guidelines
Search for vulnerabilities resulting from the violation of this guideline on the CERT website.
Other Languages
This guideline is similar to the C Secure Coding Standard recommendation : ERR02-C. Avoid in-band error indicators and to the
C++ Secure Coding Standard recommendation : ERR02-CPP. Avoid in-band error indicators
Bibliography
[Ware 2008]
[MITRE 2009] CWE ID 393, "Return of Wrong Status Code," and CWE ID 389, "Error Conditions, Return Values, Status Codes"
...