Bug 25389 - File(new URI("file:./")) -> java.lang.NullPointerException
File(new URI("file:./")) -> java.lang.NullPointerException
Status: RESOLVED FIXED
Product: classpath
Classification: Unclassified
Component: classpath
0.20
: P3 normal
: 0.20
Assigned To: Tom Tromey
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2005-12-13 11:20 UTC by Caolan McNamara
Modified: 2005-12-14 18:37 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-12-14 00:51:32


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Caolan McNamara 2005-12-13 11:20:15 UTC
A regression in 4.1.0 which is not in 4.0.2, brings OOo java component registration to its knees. Sample source inline

import java.net.URI;
import java.io.File;

class myfirstprog
{
        public static void main(String args[])
        {
           URI uriObject = null;
           try {
               uriObject = new URI("file:./");
           }
           catch (Exception e) {
           }
           System.err.println("urlObject is " + uriObject);
           File uriFile = null;
           uriFile = new File(uriObject);
           System.err.println("uriFile is " + uriFile);
        }

}

output from 4.1.0 is...
urlObject is file:./
Exception in thread "main" java.lang.NullPointerException
   <<No stacktrace available>>

output from 4.2.0 was...
urlObject is file:./
uriFile is .
Comment 1 Mark Wielaard 2005-12-13 11:46:25 UTC
Confirmed. URI.getPath() may return null and we don't check for that in the File(URI) constructor. A simple fix might be:

diff -u -r1.59 File.java
--- java/io/File.java   6 Nov 2005 20:28:00 -0000       1.59
+++ java/io/File.java   13 Dec 2005 11:37:26 -0000
@@ -406,7 +406,11 @@
     if (!uri.getScheme().equals("file"))
        throw new IllegalArgumentException("invalid uri protocol");

-    path = normalizePath(uri.getPath());
+    String name = uri.getPath();
+    if (name == null)
+      name = "";
+    
+    path = normalizePath(name);
   }

   /**


Note that java/io/File.java is not fully merged between classpath and libgcj.
Comment 2 Mark Wielaard 2005-12-13 13:47:33 UTC
Added a test to mauve that exposes this bug:
gnu/testlet/java/io/File/newFileURI.java
Comment 3 Tom Tromey 2005-12-13 20:07:31 UTC
with the JDK I get this output:

opsy. java myfirstprog
urlObject is file:./
Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierarchical
        at java.io.File.<init>(File.java:344)
        at myfirstprog.main(myfirstprog.java:16)


Strange.
Comment 4 Mark Wielaard 2005-12-13 22:01:37 UTC
(In reply to comment #3)
> Exception in thread "main" java.lang.IllegalArgumentException: URI is not
> hierarchical
>         at java.io.File.<init>(File.java:344)
>         at myfirstprog.main(myfirstprog.java:16)

Interesting. That is a bit lame. But seems legal since the uri scheme isn't followed by a double slash so it is indeed not hierarchical.

But since we are constructing such URLs/URIs ourselves (for example in the SystemClassLoader) through new URL("file", "", -1, "./") it seems wise to handle them explicitly in the File(URI) constructor.
Comment 5 Tom Tromey 2005-12-14 00:51:01 UTC
URLs are handled differently.
If you do new File("./").toURI() you will get a URI
with the absolute path name in it -- i.e., a
hierarchical one.  The same happens for
new File("./").toURL().toURI()

Caolan -- if we changed this to throw an IllegalArgumentException,
would that help?  I'm wondering how this code can work in OO.o.
Another way it could work is if there is variance between JDK versions
and you've only happened to try ones where it works somehow.

Comment 6 Tom Tromey 2005-12-14 00:51:32 UTC
I'm handling this.
Comment 7 Caolan McNamara 2005-12-14 08:35:09 UTC
Throwing IllegalArgumentException should be fine. 
Comment 8 Tom Tromey 2005-12-14 18:35:43 UTC
Subject: Bug 25389

Author: tromey
Date: Wed Dec 14 18:35:37 2005
New Revision: 108527

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=108527
Log:
	PR classpath/25389:
	* java/io/File.java (File): Throw IllegalArgumentException if URI is
	non-hierarchical.

Modified:
    branches/gcc-4_1-branch/libjava/ChangeLog
    branches/gcc-4_1-branch/libjava/java/io/File.java

Comment 9 Tom Tromey 2005-12-14 18:36:58 UTC
Subject: Bug 25389

Author: tromey
Date: Wed Dec 14 18:36:55 2005
New Revision: 108528

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=108528
Log:
	PR classpath/25389:
	* java/io/File.java (File): Throw IllegalArgumentException if URI is
	non-hierarchical.

Modified:
    trunk/libjava/ChangeLog
    trunk/libjava/java/io/File.java

Comment 10 Tom Tromey 2005-12-14 18:37:37 UTC
Fix in gcc 4.1, gcc trunk, and classpath cvs head.
Comment 11 cvs-commit@developer.classpath.org 2005-12-14 21:28:50 UTC
Subject: Bug 25389

CVSROOT:	/cvsroot/classpath
Module name:	classpath
Branch: 	
Changes by:	Tom Tromey <tromey@savannah.gnu.org>	05/12/14 17:32:35

Modified files:
	java/io        : File.java 
	.              : ChangeLog 

Log message:
	PR classpath/25389:
	* java/io/File.java (File): Throw IllegalArgumentException if URI is
	non-hierarchical.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/classpath/classpath/java/io/File.java.diff?tr1=1.59&tr2=1.60&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/classpath/classpath/ChangeLog.diff?tr1=1.5810&tr2=1.5811&r1=text&r2=text