...
This noncompliant code example attempts to delete a file but fails to check whether the operation has succeeded.:
Code Block | ||
---|---|---|
| ||
public void deleteFile(){ File someFile = new File("someFileName.txt"); // doDo something with someFile someFile.delete(); } |
...
This compliant solution checks the boolean
value returned by the delete()
method and handles any resulting errors.:
Code Block | ||
---|---|---|
| ||
public void deleteFile(){ File someFile = new File("someFileName.txt"); // doDo something with someFile if (!someFile.delete()) { // handleHandle failure to delete the file } } |
...
It is especially important to process the return values of immutable object methods. While Although many methods of mutable objects operate by changing some internal state of the object, methods of immutable objects cannot change the object and often return a mutated new object, leaving the original object unchanged.
...
This compliant solution correctly updates the String
reference original
with the return value from the String.replace()
method.:
Code Block | ||
---|---|---|
| ||
public class Replace { public static void main(String[] args) { String original = "insecure"; original = original.replace('i', '9'); System.out.println(original); } } |
...
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
EXP00-J | mediumMedium | probableProbable | mediumMedium | P8 | L2 |
Automated Detection
Tool | Version | Checker | Description |
---|---|---|---|
Coverity | 7.5 | CHECKED_RETURN | Implemented |
Related Guidelines
EXP12-CPP. Do not ignore values returned by functions or methods | |
Passing Parameters and Return Values [CSJ] | |
CWE-252. , Unchecked return valueReturn Value |
Bibliography
[API 2006] | |
Misusing | |
[Seacord 2015] | EXP00-J. Do not ignore values returned by methods LiveLesson |
...