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

File returns wrong info when file names contain 0x0000 chars (unix)

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P5 P5
    • None
    • 1.4.0
    • core-libs
    • x86
    • linux

      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)
      ======================================================================

            alanb Alan Bateman
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: