United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-8025128 File.createTempFile fails if prefix is absolute path
JDK-8025128 : File.createTempFile fails if prefix is absolute path

Details
Type:
Bug
Submit Date:
2013-09-20
Status:
Closed
Updated Date:
2013-10-22
Project Name:
JDK
Resolved Date:
2013-09-28
Component:
core-libs
OS:
windows_7
Sub-Component:
java.io
CPU:
Priority:
P2
Resolution:
Fixed
Affected Versions:
7u40
Fixed Versions:

Related Reports
Backport:
Backport:
Backport:

Sub Tasks

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>".
                                    

Comments
Dan is looking into this to see if a fix is required (specifying the usage as "/foo" is of course not the intended usage of this API but the validation in previous releases didn't catch it).
                                     
2013-09-26
One other software packages are facing this bug:
Pentaho: http://forums.pentaho.com/showthread.php?149302-Error-installing-timeseriesForecasting-plugin
(removed Alfresco, not applicable)
                                     
2013-09-26
You are correct, let me remove that from my comment. 
                                     
2013-09-26
URL:   http://hg.openjdk.java.net/jdk8/tl/jdk/rev/754db1268be1
User:  dxu
Date:  2013-09-28 00:10:19 +0000

                                     
2013-09-28
URL:   http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/754db1268be1
User:  lana
Date:  2013-10-11 18:24:46 +0000

                                     
2013-10-11



Hardware and Software, Engineered to Work Together