JDK-8025128 : File.createTempFile fails if prefix is absolute path
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 7u40
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_7
  • Submitted: 2013-09-20
  • Updated: 2014-02-12
  • Resolved: 2013-09-28
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availabitlity Release.

To download the current JDK release, click here.
JDK 7 JDK 8
7u45Fixed 8 b112Fixed
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
You are correct, let me remove that from my comment.
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

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