You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 50 Next »

Methods return values to communicate failure or success and at other times, to update the caller's objects or fields. Security risks can arise if return values are simply ignored or if suitable action is not taken on their receipt. Return values may be ignored intentionally or even unintentionally. For example, when getter methods that return a value are named after an action, such as ProcessBuilder.redirectErrorStream(), a programmer may not realize that a return value is expected. Incidentally, the only purpose of the redirectErrorStream() method is to tell using a return value, whether the process builder merges standard error and standard output. The action of actually redirecting the error stream is performed by its overloaded single argument version. It is important to read the API documentation so that return values are not ignored.

Unknown macro: {mc}

Another example is ignoring the return value from add() on a HashSet. If duplicate, false will be returned.

Noncompliant Code Example

This noncompliant code example attempts to delete a file, but does not check whether the operation has succeeded.

  File someFile = new File("someFileName.txt");
  // do something with someFile
  someFile.delete();

Compliant Solution

This compliant solution checks the (boolean) value returned by the delete() method and, if necessary, handles the error.

  File someFile = new File("someFileName.txt");
  // do something with someFile
  if (!someFile.delete()) {
    // handle the fact that the file has not been deleted
  }

Noncompliant Code Example

This noncompliant code example ignores the return value of the String.replace method. As a result, the original string is not updated even though it seems otherwise. The String.replace() method does not modify the state of the String but instead, returns a reference to a new String object with the replacements in place.

public class Ignore {
  public static void main(String[] args) {
    String original = "insecure";
    original.replace( 'i', '9' );
    System.out.println(original);
  }
}

Compliant Solution

This compliant solution correctly updates the original String object by assigning to it the return value.

public class DoNotIgnore {
  public static void main(String[] args) {
    String original = "insecure";
    original = original.replace( 'i', '9' );
    System.out.println(original);
  }
}

Another source of security vulnerabilities caused by ignoring return values is detailed in guideline [FIO02-J. Keep track of bytes read and account for character encoding while reading data].

Risk Assessment

Ignoring method return values can lead to unanticipated program behavior.

Guideline

Severity

Likelihood

Remediation Cost

Priority

Level

EXP00-J

medium

probable

medium

P8

L2

Automated Detection

The Coverity Prevent Version 5.0 CHECKED_RETURN checker can detect the instance where Value returned from a function is not checked for errors before being used.

Related Vulnerabilities

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

Other Languages

This guideline appears in the C Secure Coding Standard as [seccode:EXP12-C. Do not ignore values returned by functions].

This guideline appears in the C++ Secure Coding Standard as [cplusplus:EXP12-CPP. Do not ignore values returned by functions or methods].

Bibliography

[[API 2006]] method delete()
[[API 2006]] method replace()
[[Green 2008]] "String.replace"
[[Pugh 2009]] misusing putIfAbsent
[[MITRE 2009]] CWE ID 252 "Unchecked Return Value"


[!The CERT Oracle Secure Coding Standard for Java^button_arrow_left.png!]      [!The CERT Oracle Secure Coding Standard for Java^button_arrow_up.png!]      [!The CERT Oracle Secure Coding Standard for Java^button_arrow_right.png!]

  • No labels