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

Compare with Current View Page History

« Previous Version 32 Next »

The conditional AND and OR operators (&& and ||, respectively) exhibit short-circuit behavior. That is, the second operand is not evaluated if the result can be deduced solely by evaluating the first operand.

Exercise caution if 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 not immediately obvious whether i gets incremented or not.

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 program 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]] exemplifiesin the following example:

if (data != null && i < data.length && data[i] != -1) ... 

This code snippet sequentially executes the subexpressions while avoiding an array indexing exception resulting from the checks that execute prior to the last subexpression.

Risk Assessment

Failing 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

TODO

Related Vulnerabilities

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

References

[[JLS 2005]] Sections 15.23 "Conditional-And Operator &&" and 15.24 "Conditional-Or Operator ||"
[[Flanagan 2005]] 2.5.6. Boolean Operators


EXP06-J. Use parentheses for precedence of operation      04. Expressions (EXP)      EXP08-J. Understand the evaluation of expressions containing non short-circuit operators

  • No labels