[Kaffe] bug found and fixed in java.io.File!

Moses DeJong dejong at cs.umn.edu
Tue Jan 26 21:33:49 PST 1999


On Tue, 26 Jan 1999, Archie Cobbs wrote:

> Moses DeJong writes:
> > > Your patches are appreciated but don't exactly implement what's
> > > described in:
> > > 
> > >   http://java.sun.com/products/jdk/1.2/docs/api/index.html
> > 
> > Well, I guess I missed that one. I think this will fix it.
> > 
> > public File(File dir, String name) {
> >     this( (dir == null) ? null : dir.getPath() , name);
> > }
> > 
> > Did you find any other problems with the patch?
> 
> No, but that's because I was lazy and stopped there.. :-)
> 
> Can you send an updated patch (diff -u please)?
> 
> Thanks,
> -Archie



Ok, here is the "new and improved" version of the File.java patch.
You were correct that the null argument case was changed in JDK 1.2.
It seems like that was a bug in the JDK1.1 so I changed the kaffe
patch so that it works like JDK1.2 now. Here is a diff -u format patch.




--- old_kaffe/libraries/javalib/java/io/File.java       Wed Dec  9 17:20:10 1998
+++ kaffe/libraries/javalib/java/io/File.java   Tue Jan 26 23:23:36 1999
@@ -26,23 +26,33 @@
 }
 
 public File(File dir, String name) {
-       if (dir == null) {
-               this.path = name;
+        this((dir == null) ? null : dir.getPath(), name);
+}
+
+public File(String path, String name) {
+       if (name == null) {
+            throw new NullPointerException();
+        }
+       if (path == null) {
+           this.path = name;
+       } else if (path.length() == 0) {
+           this.path = separatorChar + name;
        }
        else {
-               this.path = dir.getPath() + separatorChar + name;
+            char last = path.charAt( path.length() - 1 );
+            if (last != separatorChar) {
+                this.path = path + separatorChar + name;
+            } else {
+                this.path = path + name;
+            }
        }
 }
 
 public File(String path) {
-       this.path = path;
-}
-
-public File(String path, String name) {
        if (path == null) {
-               path = ".";
-       }
-       this.path = path + separatorChar + name;
+            throw new NullPointerException();
+        }
+       this.path = path;
 }
 
 public boolean canRead() {





The test case I was using.



import java.io.File;

public class TestFile {

    public static void exists(File f) {
      String type = "File";
      if (f.isDirectory()) { type = "Directory"; }
      System.out.print(type + " \"" + f.getPath() );
      if (f.exists()) {
          System.out.println("\" does exist");
      } else {
          System.out.println("\" does not exist");
      }
    }

    public static void main(String[] argv) throws Exception {
        File g1 = new File("/", "tmp"); exists(g1);
        File g2 = new File("/"); exists(g2);
        File g3 = new File(g2, "tmp"); exists(g3);
        File g4 = new File(""); exists(g4);
        File g5 = new File("","file2"); exists(g5);
	File g6 = new File((String)null,"file2"); exists(g6);

        try {
        File b1 = new File(null); exists(b1);
        } catch (NullPointerException e) {System.out.println("caught 1");}

        try {
        File b2 = new File("/", null); exists(b2);
        } catch (NullPointerException e) {System.out.println("caught 2");}

	try {
	File b3 = new File((File)null,"file2"); exists(b3);
        } catch (NullPointerException e) {System.out.println("caught 3");}

    }
}






JDK 1.1 output

Directory "/tmp" does exist
Directory "/" does exist
Directory "/tmp" does exist
File "" does not exist
File "/file2" does not exist
File "file2" does not exist
caught 1
caught 2
caught 3


JDK 1.2 output

Directory "/tmp" does exist
Directory "/" does exist
Directory "/tmp" does exist
File "" does not exist
File "/file2" does not exist
File "file2" does not exist
caught 1
caught 2
File "file2" does not exist



Kaffe with my new patch

Directory "/tmp" does exist
Directory "/" does exist
Directory "/tmp" does exist
File "" does not exist
File "/file2" does not exist
File "file2" does not exist
caught 1
caught 2
File "file2" does not exist



More information about the kaffe mailing list