JDK-4751975 : File returns wrong info when file names contain 0x0000 chars (unix)
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.4.0
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2002-09-24
  • Updated: 2009-02-16
  • Resolved: 2009-02-16
Related Reports
Duplicate :  
Description
Name: nt126004			Date: 09/23/2002


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 OPERATING SYSTEM VERSION :
glibc-2.2.5-39
kernel 2.4.19
RedHat Linux 7.3, fully up2date as of this report

A DESCRIPTION OF THE PROBLEM :
The java.io.File class reports incorrect information for a
file object if zero bytes are included in the file name.

I stumbled across this when I read a directory name from a
file.  The name included trailing zeros which I accidentally
included in the String object representing the directory name.

Creating a File object from this name seemed to work
correctly, however using that File object to create new File
objects to represent the names of files/directories within
that directory causes erronous results.

The response to isFile(), isDirectory(), exists(), etc all
seem to be giving information for the top level directory
rather then the referenced files.


EXPECTED VERSUS ACTUAL BEHAVIOR :
Obviously, this is a garbage in garbage out problem.
However I would expect one of the following behaviors:

1: The badly formed names would always indicate that the
file  did not exists.  This seems the most logical.

OR

2: The zeros in the file names would always be truncated and
ignored.

REPRODUCIBILITY :
This bug can be reproduced always.

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

public class test
{
    static public void main( String args[] )
    {
	if( args.length < 1 ){
	    System.out.println( "Please pass the name of a directory" );
	    return;
	}

	File passed = new File( args[0] );
	if( !passed.isDirectory() ){
	    System.out.println( "Please pass the name of a real directory" );
	    return;
	}

	File bogus = new File( passed, "BogusFileName" );
	if( bogus.exists() )
	    System.out.println( "File: [" + bogus + "] does exists" );
	else
	    System.out.println( "File: [" + bogus + "] does NOT exists" );

	// Now, try with a zero byte in the path name
	int len = args[0].length();
	byte buff[] = new byte[ len + 1 ];
	System.arraycopy( args[0].getBytes(), 0, buff, 0, len );
	buff[len] = 0;

	File passedWithZero = new File( new String(buff) );
	System.out.println( "Passed dir name with zero is: " + passedWithZero );
	System.out.println( "   Exists: " + passedWithZero.exists() );
	System.out.println( "   is dir: " + passedWithZero.isDirectory() );
	System.out.println( "  is file: " + passedWithZero.isFile() );

	File bogusZero = new File( passedWithZero, "BogusFileName" );
	System.out.println( "Bogus file name with zero is: " + bogusZero );
	System.out.println( "   Exists: " + bogusZero.exists() );
	System.out.println( "   is dir: " + bogusZero.isDirectory() );
	System.out.println( "  is file: " + bogusZero.isFile() );
	
	
    }
}

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

CUSTOMER WORKAROUND :
Don't make the stupid mistake of including the zeros in the
file names!
(Review ID: 164504) 
======================================================================

Comments
EVALUATION This feature has been addressed by the new file system API defined by JSR-203.
16-02-2009

EVALUATION The basic question is what the behaviour should be if the abstract pathname represented by the File object would have an invalid syntax for a physical object on the file system. One choice would be to throw an exception for clearly impossible/invalid file names (e.g. paths with invalid characters) at creation time. Unfortunately, we can't do this for the existing File class for compatibility reasons. JSR-203 is expected to provide new APIs to represent the file-system. It may choose to implement such a validation scheme. The only choice I can see for the current code is to document that problems with the pathname will not be detected until a file-system operation is preformed. If it is possible to check the path syntax, then it would make sense to add an API File.isValidPath() for convenience.
28-11-2005