Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8230840

java.io.File.renameTo checks for null argument too late

XMLWordPrintable

    • x86_64
    • windows_10

      A DESCRIPTION OF THE PROBLEM :
      As per source:
      public boolean renameTo(File dest) {
              SecurityManager security = System.getSecurityManager();
              if (security != null) {
                  security.checkWrite(path);
                  security.checkWrite(dest.path);
              }
              if (dest == null) {
                  throw new NullPointerException();
              }
              if (this.isInvalid() || dest.isInvalid()) {
                  return false;
              }
              return fs.rename(this, dest);
          }

      Only after dest.path is accessed is desc compared to null, causing a NPE before the expected place.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Call File.renameTo on any file with a null argument.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A NullPointerException should be thrown from the throw clause.
      ACTUAL -
      A NPE was thrown due to a null argument in the call security.checkWrite(dest.path).

      ---------- BEGIN SOURCE ----------
      package bug;
      import java.io.File;

      public class Main{
          public static void main(String...args){
              new File("C:"+File.separator+"temp").renameTo(null);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      None.

      FREQUENCY : always


            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: