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

File.createTempFile fails if prefix is absolute path

    XMLWordPrintable

Details

    • b112
    • windows_7
    • Verified

    Backports

      Description

        FULL PRODUCT VERSION :
        java version "1.7.0_40"
        Java(TM) SE Runtime Environment (build 1.7.0_40-b43)
        Java HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Windows 7
        Fedora
        OSX

        EXTRA RELEVANT SYSTEM CONFIGURATION :
        nothing specific - happens on all OS

        A DESCRIPTION OF THE PROBLEM :
        createTempFile in past worked fine with "/foo" as input but is now failing.

        REGRESSION. Last worked in version 7u40

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        This passed on all JDK's except 1.7.0_40:

        package tempfile;

        import java.io.File;
        import java.io.IOException;

        public class Main {

        public static void main(String[] args) throws IOException {
        String fileName= "/foo";
        File target = File.createTempFile(fileName, ".jar", new File("."));
        System.out.println(target.getCanonicalPath() + " " + target.exists());
        }
        }





        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        What is expected is a random named file is created in the current directory with base name "foo".

        Like "/Users/max/workspace/j1/tempfile/foo3720102051149040684.jar"
        ACTUAL -

        On 1.7.0_40 this breaks with the error:
        Exception in thread "main" java.io.IOException: Unable to create temporary file
        at java.io.File$TempDirectory.generateFile(File.java:1891)
        at java.io.File.createTempFile(File.java:1979)
        at tempfile.Main.main(Main.java:10)

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        package tempfile;

        import java.io.File;
        import java.io.IOException;

        public class Main {

        public static void main(String[] args) throws IOException {
        String fileName= "/foo";
        File target = File.createTempFile(fileName, ".jar", new File("."));
        System.out.println(target.getCanonicalPath() + " " + target.exists());
        }
        }




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

        CUSTOMER SUBMITTED WORKAROUND :
        Workaround is to change existing code to remove "/" from the passed on prefix.

        But it currently breaks Eclipse, JBoss Tools and Developer Studio:
        https://issues.jboss.org/browse/JBIDE-15494
        https://bugs.eclipse.org/bugs/show_bug.cgi?id=417043

        The fix made for JDK-8011950 at http://hg.openjdk.java.net/jdk8/tl/jdk/rev/4a66dd1d7eea fixes the issue about relative paths like ../ being used in the prefix but in doing so it broke the previous existing and already safe guarded input of "/<somename>".

        Attachments

          Issue Links

            Activity

              People

                dxu Dan Xu (Inactive)
                ndcosta Nelson Dcosta (Inactive)
                Votes:
                0 Vote for this issue
                Watchers:
                7 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: