JDK-5043449 : File.createNewFile() succeeds for reserved words and no File exists (win)
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 6
  • Priority: P4
  • Status: Resolved
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-05-06
  • Updated: 2013-07-11
  • Resolved: 2013-07-11
Related Reports
Relates :  
Description
Name: rmT116609			Date: 05/06/2004


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

java version "1.5.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b32c)
Java HotSpot(TM) Client VM (build 1.5.0-beta-b32c, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
When i ty to create some file names with windows reserved words.It wont report any exception and program flow goes as if it created the file.
If i run the below code snippet it neither created the file LPT1.txt nor it thorwed exception to putput.

it prints the statement "created the file name LPT1" as if it created.


String windowreservedWords[] ={"LPT1.txt","CON.txt","COM1.txt")
try{
File file=new File(windowreservedWords[0]);
file.createNewFile();
if(file.exists()){

System.out.println("created File name"+windowreservedWords[0]);

}
}catch(IOException ioe){
e.printStackTrace();
}

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the test case provided.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
It must throw IOException
ACTUAL -
does nothing.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------

import java.io.*;

class Test {

public static void main(String args[]) {

String windowreservedWords[] ={"LPT1.txt","CON.txt","COM1.txt"};
try{
File file=new File(windowreservedWords[0]);
file.createNewFile();
if(file.exists()){

System.out.println("created File name"+windowreservedWords[0]);

}
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Provide method in the System class to get all reserved words in the OS and restrict it which will be helpful for other checks too.
(Incident Review ID: 261243) 
======================================================================
###@###.### 10/8/04 19:21 GMT

Comments
The return value of createNewFile() is used here, not exception, to indicate whether the create operation is successful.
11-07-2013

EVALUATION For winNT, we are calling the win32 API CreateFileW. It looks like it does return a valid handle for these devices. Perhaps there is win32 API which allows us to determine if the filename is valid?
27-10-2005

EVALUATION This is a standard windows oddity that general-purpose I/O frameworks like java's bump into eventually. Java may need to know about all the windows reserved names. My favorite such name is AUX. ###@###.### 2004-05-06 We stumbled on a way to inhibit "windows reserved names interpretation" by the Win32 API. Prefixing the magic string "\\?\" to a filename indicates that the following characters are to be interpreted as a canonical absolute windows pathname. Since this is also the solution to the MAX_PATH limit, one thing we could do to get reliable Windows I/O is to always convert filenames to absolute canonical form, prepend "\\?\" and only then pass it to the Win32 API. Also worth considering is whether the user might actually *want* to use the special windows interpretation. If a user does new FileOutputStream(new File("LPT1")), should that end up going to a real line printer?? See: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/naming_a_file.asp ###@###.### 2004-12-17 20:37:37 GMT ###@###.### 2005-2-14 22:20:27 GMT
17-12-2004

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mustang
14-06-2004