The conditional AND and OR operators (&&
and ||
, respectively) exhibit short-circuit behavior. That is, the second operand is evaluated only when the result of the conditional operator cannot be deduced solely by evaluating the first operand. Consequently, when the result of the conditional operator can be deduced solely from the result of the first operand, the second operand will remain unevaluated; its side-effects, if any, will never occur.
In the following code, the value of i
is incremented only when i >= 0
.
int i = // initialize to user supplied value if ((i >= 0) && ((i++) <= Integer.MAX_VALUE)) { // ... }
Although the behavior is well defined, it is unclear whether i
is incremented on any particular execution.
Noncompliant Code Example
This noncompliant code example attempts to rename a given file if it exists, perform operations on the renamed file, and then delete the renamed file. For the purposes of this example, we assume that the file operations are performed on a secure portion of the filesystem. Consequently, we need not consider the time-of-check to time-of-use vulnerabilities that would otherwise be of concern.
When the exists()
method returns true
, execution continues with the call to File.delete()
; the call to File.renameTo()
remains unexecuted in this case. Consequently, the code as written fails to guarantee the existence of the renamed file, which can result in an attempt to use and subsequently to delete a nonexistent file. Further, File.delete()
returns an error code on failure rather than throwing an exception; this noncompliant example incorrectly ignores the return value from File.delete()
(see guideline EXP00-J. Do not ignore values returned by methods).
class BadRenameFile { public static void main(String[] args) { File fOriginal = new File("original.txt"); File fNew = new File("new.txt"); if (fOriginal.exists() || fOriginal.renameTo(fNew)) { // do something with fNew fNew.delete(); } } }
Compliant Solution
This compliant solution checks both for the existence of the original file as well as for the success of the rename operation. It also handles the error when either check fails. It deletes the new file when operations are complete, checking for and handling the possible failure of the delete operation.
class RenameFile { public static void main(String[] args) { File fOriginal = new File("original.txt"); File fNew = new File("new.txt"); if (!fOriginal.exists()) { // handle error } if (!fOriginal.renameTo(fNew)) { // handle error } // do something with fNew if (!fNew.delete()) { // handle error } } }
When error handling for failure of the existence and rename operations is identical, that portion of the code could be rewritten as:
if (!fOriginal.exists() || !fOriginal.renameTo(fNew)) { // handle error }
In this form, the short-circuit behavior of the ||
operation provides identical semantics to the "two-if-statement" version shown above.
Exceptions
EXP06-EX1: Programmers who are aware of the short-circuit behavior often use it to their advantage, as in this example from Flanagan [[Flanagan 2005]]:
if (data != null && i < data.length && data[i] != -1) ...
This code snippet sequentially checks for potential error conditions before allowing the main computation to proceed. The short-circuit behavior of &&
guarantees that the first error condition encountered will terminate the checking process.
Risk Assessment
Failure to understand the short-circuit behavior of the logical AND and OR operators can cause unintended program behavior.
Guideline |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
EXP07-J |
low |
unlikely |
medium |
P2 |
L3 |
Automated Detection
Detection of short-circuit operators is straightforward, but sound error checking for this guideline is not feasible in the general case. Heuristic warnings could be useful.
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this guideline on the CERT website.
Related Guidelines
CERT C Secure Coding Standard: EXP02-C. Be aware of the short-circuit behavior of the logical AND and OR operators
CERT C++ Secure Coding Standard: EXP02-CPP. Be aware of the short-circuit behavior of the logical AND and OR operators
Bibliography
[[Flanagan 2005]] 2.5.6. Boolean Operators
[[JLS 2005]] Sections 15.23 "Conditional-And Operator &&" and 15.24 "Conditional-Or Operator ||"
EXP06-J. Use parentheses for precedence of operation 04. Expressions (EXP) EXP08-J. Understand the evaluation of expressions containing non-short-circuit operators