Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: text edits, removed NCE/CS

...

This noncompliant code example is designed attempts to rename a given file if the file to be renamed is presentit exists, perform operations on the renamed file, and then delete the renamed file. However, due to because of the short-circuit behavior of the || operator, the renameTo() method appearing as the second argument to || is not executed when the exists() method appearing as the first argument to || 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 often silently ignored or perceived as unnecessary. (See EXP02-J. Do not ignore values returned by methods)

Code Block
bgColor#ffcccc
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, which is not guaranteed to exist. fNew will only 
      // exist if file 'new.txt' exists, which has not been checked by this program.
      // ...
      // fNew may not exist as renameTo() may not have been successfully executed and 
      // the existence of 'new.txt' has not been checked.
      fNew.delete();
    }
  }
} 

Compliant Solution

...

Code Block
bgColor#ccccff
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  
    }
  }
} 

Noncompliant Code Example

This noncompliant example differs from the previous one in that, there are no side effects in the right hand side operand. Nevertheless, an unaware programmer can get caught in the short-circuit behavior of the conditional AND and OR operators. The programmer has combined two expressions in the if statement. The first checks whether the d object is null and the second checks if the default security manager has been installed (by comparing sm with null). If both d and sm are not null, the security check will be performed. This is a case of trying to combine together two null checks into one statement.

The result of this expression is that if d is equal to null, the if expression evaluates to false and the security check is not executed.

Code Block
bgColor#ffcccc

// d = null
SecurityManager sm = System.getSecurityManager();	  
if(d != null && sm != null) {
  FilePermission perm = new FilePermission("file.dat", "read");
  sm.checkPermission(perm);
  /* do something with d */
}
/* read the file (skips security check) */

Noncompliant Code Example

In this example, the programmer switches the ordering of the two subexpressions and uses a || operator to ensure the security check is carried out but does not realize that the second check is evaluated if the first one succeeds. The result is the inadvertent modification of d.

Code Block
bgColor#ffcccc

SecurityManager sm = System.getSecurityManager();	  
if(sm != null || d != null) {
  FilePermission perm = new FilePermission("file.dat", "read");
  sm.checkPermission(perm);
  // do something with d
}
// read the file

Compliant Solution

Decouple distinct operations that use the conditional AND and OR operators from expressions constituting decision statements. When this is not possible, be aware of the short-circuit behavior and code accordingly.

Code Block
bgColor#ccccff

if(d != null) { /* do something with d */ }

if(sm != null) {
  FilePermission perm = new FilePermission("file.dat", "read");
  sm.checkPermission(perm);
}
/* read the file (default security check is carried out) */

Exceptions

Wiki Markup
*EXP06-J-EX1:* Sometimes programmers who are aware of the short-circuit behavior use it to their advantage, as Flanagan \[[Flanagan 05|AA. Java References#Flanagan 05]\] exemplifies:

...