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

File.renameTo(File dest) should check for NPE at the very beginning

XMLWordPrintable

    • b04
    • generic
    • generic
    • Not verified

      A DESCRIPTION OF THE PROBLEM :
      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);
          }

      if a SecurityManager is exists, and dest is null, then execute security.checkWrite(dest.path) will throw nullpoint exception, and if (dest == null) is no meaning.

      the first step should be test if dest is null.

      public boolean renameTo(File dest) {
              if (dest == null) {
                  throw new NullPointerException();
              }
              SecurityManager security = System.getSecurityManager();
              if (security != null) {
                  security.checkWrite(path);
                  security.checkWrite(dest.path);
              }
              if (this.isInvalid() || dest.isInvalid()) {
                  return false;
              }
              return fs.rename(this, dest);
          }


      FREQUENCY : always


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

              Created:
              Updated:
              Resolved: