public inbox for java-prs@sourceware.org
help / color / mirror / Atom feed
* [Bug java/34369]  New: java.net.URI.relativize(URI) method returns incorrect results
@ 2007-12-06 22:10 lnx1138 at us dot ibm dot com
  2007-12-06 22:12 ` [Bug java/34369] " lnx1138 at us dot ibm dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: lnx1138 at us dot ibm dot com @ 2007-12-06 22:10 UTC (permalink / raw)
  To: java-prs

The method java.net.URI.relativize(URI) returns incorrect results for a couple
of cases when compared to implementations from IBM Java or Sun Java.

This problem affects directly the debugging process of C projects on Eclipse.
Whenever someone is debugging a project whose part of its name is the
name of another project on the workspace, Eclipse doesn't find the correct path
for the project source files. For instance, if we have a project named "a" and
another project named "aa" and we try to debug a file on "aa", Eclipse will
match the path for the file on "aa" with the path to the project "a" and will
not find the file as it doesn't exist on project "a".

The method documentaion at
http://java.sun.com/j2se/1.4.2/docs/api/java/net/URI.html#relativize(java.net.URI)
says:

 The relativization of the given URI against this URI is computed as follows:

   1. If either this URI or the given URI are opaque, or if the scheme and
authority components of the two URIs are not identical, or if the path of this
URI is not a prefix of the path of the given URI, then the given URI is
returned.

   2. Otherwise a new relative hierarchical URI is constructed with query and
fragment components taken from the given URI and with a path component computed
by removing this URI's path from the beginning of the given URI's path.

The test for "if the path of this URI is not a prefix of the path of the given
URI then the given URI is returned" in libjava/classpath/java/net/URI.java does
not appear correct:

  public URI relativize(URI uri)
  {
    if (isOpaque() || uri.isOpaque())
      return uri;
    if (scheme == null && uri.getScheme() != null)
      return uri;
    if (scheme != null && !(scheme.equals(uri.getScheme())))
      return uri;
    if (rawAuthority == null && uri.getRawAuthority() != null)
      return uri;
    if (rawAuthority != null && !(rawAuthority.equals(uri.getRawAuthority())))
      return uri;
    if (!(uri.getRawPath().startsWith(rawPath)))  <---- this test
      return uri;

In some cases I would expect uri.getRawPath().startsWith(rawPath) to return
true since with a simple string character sequence test of startsWith() such as
with the string 

"file:/home/eclipse/runtime-New_configuration/simple"

would be a prefix of

"file:/home/eclipse/runtime-New_configuration/simple_spu/simple_spu.c"

even though simple and simple_spu are technically different paths. This I think
is where the other VM implementations must be doing it correctly as they likely
examine components of a path as opposed to just a string prefix check.

A second problem is that when the code drops through to 

        return new URI(null, null,
                       uri.getRawPath().substring(rawPath.length()),
                       uri.getRawQuery(), uri.getRawFragment());

the substring() method may end up returning a leading '/' on the path in some
cases which all the other JVMs do not.

The proposed solution is to create a new path with '/' appended to this.rawPath
for the given base URI we are checking for. We would have to check that it
isn't
already the last character. Example:

"file:/home/eclipse/runtime-New_configuration/simple"

becomes 

"file:/home/eclipse/runtime-New_configuration/simple/"

which is stored in basePath and then the

if (!(uri.getRawPath().startsWith(basePath)))

should end up returning uri since it doesn't have basePath as prefix when it is
for the path 
"file:/home/eclipse/runtime-New_configuration/simple_spu/simple_spu.c" for
example. 

In addition, to correct avoiding returning a leading '/' a one line change is
done to uri.getRawPath().substring(rawPath.length()) to instead get the length
of the basePath string as it should always conclude with a '/'.

A testcase is supplied to provide examples of the results from various calls to
the URI.relativize() method. Here is the results from the unpatched, failing
code as it is right now:

[eclipse@chavez ~]$ java Test
_spu/simple_spu.c
<>
eclipse
/runtime-New_configuration/simple

Here are the results after applying the patch and re-running the testcase:

[eclipse@chavez ~]$ java Test
file:/home/eclipse/runtime-New_configuration/simple_spu/simple_spu.c
<>
eclipse
runtime-New_configuration/simple

The above output is consistent with that provided by executing the testcase
using IBM Java 5, Sun Java 5 and IBM Java 1.4.2.


-- 
           Summary: java.net.URI.relativize(URI) method returns incorrect
                    results
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: java
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: lnx1138 at us dot ibm dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34369


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2008-01-21 20:11 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-12-06 22:10 [Bug java/34369] New: java.net.URI.relativize(URI) method returns incorrect results lnx1138 at us dot ibm dot com
2007-12-06 22:12 ` [Bug java/34369] " lnx1138 at us dot ibm dot com
2007-12-06 22:15 ` lnx1138 at us dot ibm dot com
2008-01-21 20:00 ` [Bug libgcj/34369] " tromey at gcc dot gnu dot org
2008-01-21 20:09 ` tromey at gcc dot gnu dot org
2008-01-21 20:09 ` tromey at gcc dot gnu dot org
2008-01-21 20:11 ` cvs-commit at developer dot classpath dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).