The conditional AND and OR operators (&&
and ||
, respectively) exhibit short-circuit behavior. That is, the second operand is evaluated only when the result cannot be deduced solely by evaluating the first operand.
Exercise caution when the operands following the first operand contain side effects. 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
gets incremented.
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. However, because of the short-circuit behavior of the ||
operator, the renameTo()
method does not execute when the exists()
method returns true
. Because of this, the renamed file may or may not exist, which may result in an attempt to use and then delete a nonexistent file. This problem is exacerbated by the fact that File.delete()
does not throw an exception but returns an error code on failure, which is sometimes silently ignored or perceived as unnecessary. (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
Knowledge of the short-circuit behavior can be used to enforce the desired specification. This compliant solution traps an error if the file does not exist or when it cannot be renamed to the new file name. Operations on the new file follow.
class RenameFile { public static void main(String[] args) { File fOriginal = new File("original.txt"); File fNew = new File("new.txt"); if (!fOriginal.exists() || !fOriginal.renameTo(fNew)) { // handle error } // do something with fNew if (!fNew.delete()) { // handle error } } }
Exceptions
EXP06-EX1: Sometimes programmers who are aware of the short-circuit behavior use it to their advantage, as Flanagan [[Flanagan 2005]] exemplifies in the following example:
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 may 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 may be useful.
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this guideline on the CERT website.
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