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

FileOutputStream(File) fails when path contains ".." (unix)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 1.4.0
    • core-libs
    • Cause Known
    • x86
    • linux

      Name: nt126004 Date: 06/17/2003


      FULL PRODUCT VERSION :
      java version "1.4.0_02"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0_02-b02)
      Java HotSpot(TM) Client VM (build 1.4.0_02-b02, mixed mode)


      FULL OS VERSION :
      Linux XXX.com 2.4.18-6mdkenterprise #1 SMP Fri Mar 15 02:28:20 CET 2002 i686 unknown

      A DESCRIPTION OF THE PROBLEM :
      On Linux
           new FileOutputStream(new File("a/b/../myFile"));
      fails with an FileNotFoundException if the directory "b" does NOT exist (but "a" DOES).
      This problem occurs on both Java 1.3 and 1.4.

      On Windows the problem does NOT occur (a/myFile is created provided a exists, regardless of the existence of b)

      In our application we were also using File.getParentFile().mkdirs() in order to create the "a" directory above. This masked the above problem since "a/b/..".mkdirs() was creating b. In 1.4.2beta (and maybe in 1.4.1 - not tested) this is no longer the case so this FileOutputStream problem was seen as a regression from 1.4) [I do NOT consider the new behaviour of mkdirs to be a bug - the old behaviour was probably wrong, this is just to illustrate how the problem can be masked]



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile test code below. [with false]
      Create directory "a"
      Execute on Linux


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      File "myFile" created in "a" directory, no exception

      ACTUAL -
      FileNotFoundException


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.io.FileNotFoundException: a/b/../myFile (No such file or directory)
      at java.io.FileOutputStream.open(Native Method)
      at java.io.FileOutputStream.<init>(FileOutputStream.java:176)
      at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
      at Test.main(Test.java:19)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.*;

      public class Test {
      public static void main(String[] args) {
              try {
                  File f = new File("a/b/../myFile");
                  System.out.println("File: " + f);
                  
                  // With false below fails on Linux on Java 1.3, 1.4
                  // With true works and 1.3 and 1.4.0, fails on 1.4.2Beta, not tested on 1.4.1
                  boolean createParent = false;
                  if (createParent) {
                      File parent = f.getParentFile();
                      boolean parentExisted = parent.exists();
                      boolean parentCreated = parent.mkdirs();
                      boolean parentExists = parent.exists();
                      System.out.println("Parent : " + parent + " existed=" + parentExisted + " created=" + parentCreated + " exists=" + parentExists);
                  }
                  FileOutputStream fos = new FileOutputStream(f);
                  fos.close();
              } catch(IOException ex) {
                  ex.printStackTrace();
              }
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      1) Do not use ".."
      or
      2) Ensure unused parent directory created
      or
      3) Used getCannonicalFile()
      (Review ID: 187079)
      ======================================================================

            Unassigned Unassigned
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: